<!DOCTYPE html>
<!-- saved from url=(0093)https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#sqlclient -->
<html class="hasSidebar hasPageActions hasBreadcrumb conceptual has-default-focus js-focus-visible theme-light mathjax-complete" lang="zh-cn" dir="ltr" data-css-variable-support="true" data-authenticated="false" data-auth-status-determined="true" data-target="docs" x-ms-format-detection="none" data-js-focus-visible=""><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta property="og:title" content="代码示例 - ADO.NET">
	<meta property="og:type" content="website">
	<meta property="og:url" content="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples">
			<meta property="og:description" content="这些示例演示 .NET Framework 程序员如何使用 ADO.NET 数据提供程序和 ADO.NET 实体框架从数据库中检索数据。">
		<meta property="og:image" content="https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png">
		<meta property="og:image:alt" content="Microsoft Logo">

	<meta name="twitter:card" content="summary">
	<meta name="twitter:site" content="@docsmsft">

	<meta name="color-scheme" content="light">


	<meta name="apiPlatform" content="dotnet">
<meta name="author" content="stevestein">
<meta name="bilingual_type" content="hover over">
<meta name="breadcrumb_path" content="/dotnet/breadcrumb/toc.json">
<meta name="depot_name" content="VS.core-docs">
<meta name="description" content="这些示例演示 .NET Framework 程序员如何使用 ADO.NET 数据提供程序和 ADO.NET 实体框架从数据库中检索数据。">
<meta name="document_id" content="9c7bb383-fcde-22f1-16fb-dd0c05a6dd35">
<meta name="document_version_independent_id" content="446292d0-2dfa-79ac-113f-3c456869b0d3">
<meta name="enable_loc_sxs" content="true">
<meta name="gitcommit" content="https://github.com/dotnet/docs.zh-cn/blob/347d98770bb0ff1002c6510b5d20b0cc8ee01f92/docs/framework/data/adonet/ado-net-code-examples.md">
<meta name="locale" content="zh-cn">
<meta name="ms.assetid" content="c119657a-9ce6-4940-91e4-ac1d5f0d9584">
<meta name="ms.author" content="sstein">
<meta name="ms.contentlocale" content="zh-CN">
<meta name="ms.date" content="03/30/2017">
<meta name="ms.devlang" content="dotnet">
<meta name="ms.lasthandoff" content="09/24/2020">
<meta name="ms.locfileid" content="91153529">
<meta name="ms.openlocfilehash" content="920912591b20102f81e1fed2f4ffbe51df8015fb">
<meta name="ms.prod" content="dotnet-framework">
<meta name="ms.sourcegitcommit" content="5b475c1855b32cf78d2d1bbb4295e4c236f39464">
<meta name="ms.technology" content="dotnet-data">
<meta name="ms.topic" content="conceptual">
<meta name="ms.translationtype" content="MT">
<meta name="original_content_git_url" content="https://github.com/dotnet/docs.zh-cn/blob/live-sxs/docs/framework/data/adonet/ado-net-code-examples.md">
<meta name="search.ms_docsetname" content="core-docs">
<meta name="search.ms_product" content="VS">
<meta name="search.ms_sitename" content="Docs">
<meta name="show_latex" content="true">
<meta name="site_name" content="Docs">
<meta name="uhfHeaderId" content="MSDocsHeader-DotNet">
<meta name="updated_at" content="2020-09-24 08:14 PM">
<meta name="page_type" content="conceptual">
<meta name="toc_rel" content="toc.json">
<meta name="pdf_url_template" content="https://docs.microsoft.com/pdfstore/zh-cn/VS.core-docs/{branchName}{pdfName}">
<meta name="search.mshattr.devlang" content="csharp vb">
<meta name="word_count" content="3375">


	<meta name="scope" content=".NET">
<link href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples" rel="canonical">
	<title>代码示例 - ADO.NET | Microsoft Docs</title>

		<link rel="stylesheet" href="./代码示例 - ADO.NET _ Microsoft Docs_files/c4247d81.site-ltr.css">

	


	<script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/saved_resource"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/saved_resource(1)"></script><script async="" defer="" crossorigin="anonymous" src="./代码示例 - ADO.NET _ Microsoft Docs_files/lux.js.下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/analytics.js.下载"></script><script id="msdocs-script">
	var msDocs = {
		data: {
			timeOrigin: Date.now(),
			contentLocale: 'zh-cn',
			contentDir: 'ltr',
			userLocale: 'zh-cn',
			userDir: 'ltr',
			pageTemplate: 'Conceptual',
			brand: '',
			context: {

			},
			hasBinaryRating: true,
			hasGithubIssues: false,
			showFeedbackReport: false,
			enableTutorialFeedback: false,
			feedbackSystem: 'None',
			feedbackGitHubRepo: '',
			feedbackProductUrl: '',
			contentGitUrl: 'https://github.com/dotnet/docs.zh-cn/blob/live/docs/framework/data/adonet/ado-net-code-examples.md',
			extendBreadcrumb: false,
			isEditDisplayable: false,
			hideViewSource: false,
			hasPageActions: true,
			hasBookmark: true,
			hasShare: true,
			hasRecommendations: false,
			contributors: [
						{ name: "stevestein", url: "https://github.com/stevestein" },
						{ name: "olprod", url: "https://github.com/olprod" },
						{ name: "Saisang", url: "https://github.com/Saisang" }
],
		},
		functions:{}
	};
	</script>
	<script src="./代码示例 - ADO.NET _ Microsoft Docs_files/wcp-consent.js.下载"></script>
	<script src="./代码示例 - ADO.NET _ Microsoft Docs_files/jsll-4.js.下载"></script>
	<script>window.jsllAwa = window.awa;</script>
	<script nomodule="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/bluebird.min.js.下载" integrity="sha384-aD4BDeDGeLXLpPK4yKeqtZQa9dv4a/7mQ+4L5vwshIYH1Mc2BrXvHd32iHzYCQy5" crossorigin="anonymous"></script>
	<script nomodule="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/fetch.umd.min.js.下载" integrity="sha384-EQIXrC5K2+7X8nGgLkB995I0/6jfAvvyG1ieZ+WYGxgJHFMD/alsG9fSDWvzb5Y1" crossorigin="anonymous"></script>
	<script nomodule="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/template.min.js.下载" integrity="sha384-1zKzI6ldTVHMU7n0W2HpE/lhHI+UG4D9IIaxbj3kT2UhCWicdTuJkTtnKuu0CQzN" crossorigin="anonymous"></script>
	<script nomodule="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/url.min.js.下载" integrity="sha384-vn7xBMtpSTfzaTRWxj0kVq0UcsbBrTOgZ/M1ISHqe1V358elYva+lfiEC+T8jLPc" crossorigin="anonymous"></script>
	<script src="./代码示例 - ADO.NET _ Microsoft Docs_files/f0a172e9.index-polyfills.js.下载"></script>
		<script src="./代码示例 - ADO.NET _ Microsoft Docs_files/qrious.min.js.下载" integrity="sha384-h/SmU/qxwYnRlzklg9PKWpDwyma9UmKuOB//Pz6Cov5KkEqcaFF0vod43/XhoeyJ" crossorigin="anonymous"></script>

		<script src="./代码示例 - ADO.NET _ Microsoft Docs_files/6a6ed3cd.index-docs.js.下载"></script><style></style>
<script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js.下载"></script><style type="text/css">.MathJax_Hover_Frame {border-radius: .25em; -webkit-border-radius: .25em; -moz-border-radius: .25em; -khtml-border-radius: .25em; box-shadow: 0px 0px 15px #83A; -webkit-box-shadow: 0px 0px 15px #83A; -moz-box-shadow: 0px 0px 15px #83A; -khtml-box-shadow: 0px 0px 15px #83A; border: 1px solid #A6D ! important; display: inline-block; position: absolute}
.MathJax_Menu_Button .MathJax_Hover_Arrow {position: absolute; cursor: pointer; display: inline-block; border: 2px solid #AAA; border-radius: 4px; -webkit-border-radius: 4px; -moz-border-radius: 4px; -khtml-border-radius: 4px; font-family: 'Courier New',Courier; font-size: 9px; color: #F0F0F0}
.MathJax_Menu_Button .MathJax_Hover_Arrow span {display: block; background-color: #AAA; border: 1px solid; border-radius: 3px; line-height: 0; padding: 4px}
.MathJax_Hover_Arrow:hover {color: white!important; border: 2px solid #CCC!important}
.MathJax_Hover_Arrow:hover span {background-color: #CCC!important}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; color: black; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_About.MathJax_MousePost {outline: none}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 2px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 2px 2em; background: transparent}
.MathJax_MenuArrow {position: absolute; right: .5em; padding-top: .25em; color: #666666; font-size: .75em}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuArrow.RTL {left: .5em; right: auto}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuCheck.RTL {right: .7em; left: auto}
.MathJax_MenuRadioCheck {position: absolute; left: 1em}
.MathJax_MenuRadioCheck.RTL {right: 1em; left: auto}
.MathJax_MenuLabel {padding: 2px 2em 4px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #CCCCCC; margin: 4px 1px 0px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: Highlight; color: HighlightText}
.MathJax_MenuDisabled:focus, .MathJax_MenuLabel:focus {background-color: #E8E8E8}
.MathJax_ContextMenu:focus {outline: none}
.MathJax_ContextMenu .MathJax_MenuItem:focus {outline: none}
#MathJax_AboutClose {top: .2em; right: .2em}
.MathJax_Menu .MathJax_MenuClose {top: -10px; left: -10px}
.MathJax_MenuClose {position: absolute; cursor: pointer; display: inline-block; border: 2px solid #AAA; border-radius: 18px; -webkit-border-radius: 18px; -moz-border-radius: 18px; -khtml-border-radius: 18px; font-family: 'Courier New',Courier; font-size: 24px; color: #F0F0F0}
.MathJax_MenuClose span {display: block; background-color: #AAA; border: 1.5px solid; border-radius: 18px; -webkit-border-radius: 18px; -moz-border-radius: 18px; -khtml-border-radius: 18px; line-height: 0; padding: 8px 0 6px}
.MathJax_MenuClose:hover {color: white!important; border: 2px solid #CCC!important}
.MathJax_MenuClose:hover span {background-color: #CCC!important}
.MathJax_MenuClose:hover:focus {outline: none}
</style><style type="text/css">.MathJax_Preview .MJXf-math {color: inherit!important}
</style><style type="text/css">.MJX_Assistive_MathML {position: absolute!important; top: 0; left: 0; clip: rect(1px, 1px, 1px, 1px); padding: 1px 0 0 0!important; border: 0!important; height: 1px!important; width: 1px!important; overflow: hidden!important; display: block!important; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none}
.MJX_Assistive_MathML.MJX_Assistive_MathML_Block {width: 100%!important}
</style><style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; -webkit-box-sizing: content-box; -moz-box-sizing: content-box; box-sizing: content-box; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
#MathJax_ZoomFrame {position: relative; display: inline-block; height: 0; width: 0}
#MathJax_ZoomEventTrap {position: absolute; left: 0; top: 0; z-index: 302; display: inline-block; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">.MathJax_Preview {color: #888}
#MathJax_Message {position: fixed; left: 1em; bottom: 1.5em; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style><style type="text/css">.MJXp-script {font-size: .8em}
.MJXp-right {-webkit-transform-origin: right; -moz-transform-origin: right; -ms-transform-origin: right; -o-transform-origin: right; transform-origin: right}
.MJXp-bold {font-weight: bold}
.MJXp-italic {font-style: italic}
.MJXp-scr {font-family: MathJax_Script,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-frak {font-family: MathJax_Fraktur,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-sf {font-family: MathJax_SansSerif,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-cal {font-family: MathJax_Caligraphic,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-mono {font-family: MathJax_Typewriter,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-largeop {font-size: 150%}
.MJXp-largeop.MJXp-int {vertical-align: -.2em}
.MJXp-math {display: inline-block; line-height: 1.2; text-indent: 0; font-family: 'Times New Roman',Times,STIXGeneral,serif; white-space: nowrap; border-collapse: collapse}
.MJXp-display {display: block; text-align: center; margin: 1em 0}
.MJXp-math span {display: inline-block}
.MJXp-box {display: block!important; text-align: center}
.MJXp-box:after {content: " "}
.MJXp-rule {display: block!important; margin-top: .1em}
.MJXp-char {display: block!important}
.MJXp-mo {margin: 0 .15em}
.MJXp-mfrac {margin: 0 .125em; vertical-align: .25em}
.MJXp-denom {display: inline-table!important; width: 100%}
.MJXp-denom > * {display: table-row!important}
.MJXp-surd {vertical-align: top}
.MJXp-surd > * {display: block!important}
.MJXp-script-box > *  {display: table!important; height: 50%}
.MJXp-script-box > * > * {display: table-cell!important; vertical-align: top}
.MJXp-script-box > *:last-child > * {vertical-align: bottom}
.MJXp-script-box > * > * > * {display: block!important}
.MJXp-mphantom {visibility: hidden}
.MJXp-munderover {display: inline-table!important}
.MJXp-over {display: inline-block!important; text-align: center}
.MJXp-over > * {display: block!important}
.MJXp-munderover > * {display: table-row!important}
.MJXp-mtable {vertical-align: .25em; margin: 0 .125em}
.MJXp-mtable > * {display: inline-table!important; vertical-align: middle}
.MJXp-mtr {display: table-row!important}
.MJXp-mtd {display: table-cell!important; text-align: center; padding: .5em 0 0 .5em}
.MJXp-mtr > .MJXp-mtd:first-child {padding-left: 0}
.MJXp-mtr:first-child > .MJXp-mtd {padding-top: 0}
.MJXp-mlabeledtr {display: table-row!important}
.MJXp-mlabeledtr > .MJXp-mtd:first-child {padding-left: 0}
.MJXp-mlabeledtr:first-child > .MJXp-mtd {padding-top: 0}
.MJXp-merror {background-color: #FFFF88; color: #CC0000; border: 1px solid #CC0000; padding: 1px 3px; font-style: normal; font-size: 90%}
.MJXp-scale0 {-webkit-transform: scaleX(.0); -moz-transform: scaleX(.0); -ms-transform: scaleX(.0); -o-transform: scaleX(.0); transform: scaleX(.0)}
.MJXp-scale1 {-webkit-transform: scaleX(.1); -moz-transform: scaleX(.1); -ms-transform: scaleX(.1); -o-transform: scaleX(.1); transform: scaleX(.1)}
.MJXp-scale2 {-webkit-transform: scaleX(.2); -moz-transform: scaleX(.2); -ms-transform: scaleX(.2); -o-transform: scaleX(.2); transform: scaleX(.2)}
.MJXp-scale3 {-webkit-transform: scaleX(.3); -moz-transform: scaleX(.3); -ms-transform: scaleX(.3); -o-transform: scaleX(.3); transform: scaleX(.3)}
.MJXp-scale4 {-webkit-transform: scaleX(.4); -moz-transform: scaleX(.4); -ms-transform: scaleX(.4); -o-transform: scaleX(.4); transform: scaleX(.4)}
.MJXp-scale5 {-webkit-transform: scaleX(.5); -moz-transform: scaleX(.5); -ms-transform: scaleX(.5); -o-transform: scaleX(.5); transform: scaleX(.5)}
.MJXp-scale6 {-webkit-transform: scaleX(.6); -moz-transform: scaleX(.6); -ms-transform: scaleX(.6); -o-transform: scaleX(.6); transform: scaleX(.6)}
.MJXp-scale7 {-webkit-transform: scaleX(.7); -moz-transform: scaleX(.7); -ms-transform: scaleX(.7); -o-transform: scaleX(.7); transform: scaleX(.7)}
.MJXp-scale8 {-webkit-transform: scaleX(.8); -moz-transform: scaleX(.8); -ms-transform: scaleX(.8); -o-transform: scaleX(.8); transform: scaleX(.8)}
.MJXp-scale9 {-webkit-transform: scaleX(.9); -moz-transform: scaleX(.9); -ms-transform: scaleX(.9); -o-transform: scaleX(.9); transform: scaleX(.9)}
.MathJax_PHTML .noError {vertical-align: ; font-size: 90%; text-align: left; color: black; padding: 1px 3px; border: 1px solid}
</style><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(1).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(2).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(3).下载"></script><style type="text/css" media="print">.usabilla_live_button_container { display: none; }</style><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(4).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(5).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(6).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(7).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(8).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(9).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(10).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(11).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(12).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(13).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(14).下载"></script><script async="" src="./代码示例 - ADO.NET _ Microsoft Docs_files/t.js(15).下载"></script><script async="" src="https://web.vortex.data.microsoft.com/collect/v1/t.js?ver=%272.1%27&amp;name=%27Ms.Webi.PageAction%27&amp;time=%272021-03-18T06%3A08%3A03.658Z%27&amp;os=%27Windows%27&amp;appId=%27JS%3ADocs%27&amp;-ver=%271.0%27&amp;-impressionGuid=%2744b91ad8-f856-451e-95f8-dcfaf1e67b0a%27&amp;-pageName=%279c7bb383-fcde-22f1-16fb-dd0c05a6dd35%27&amp;-uri=%27https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples%23sqlclient%27&amp;-pageTags=%27%7B%22author%22%3A%22stevestein%22%2C%22depotname%22%3A%22VS.core-docs%22%2C%22document_version_independent_id%22%3A%22446292d0-2dfa-79ac-113f-3c456869b0d3%22%2C%22gitcommit%22%3A%22https%3A%2F%2Fgithub.com%2Fdotnet%2Fdocs.zh-cn%2Fblob%2F347d98770bb0ff1002c6510b5d20b0cc8ee01f92%2Fdocs%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples.md%22%2C%22asst%22%3A%22c119657a-9ce6-4940-91e4-ac1d5f0d9584%22%2C%22pgauth%22%3A%22sstein%22%2C%22date%22%3A%2203%2F30%2F2017%22%2C%22pgdevlng%22%3A%22dotnet%22%2C%22lasthandoff%22%3A%2209%2F24%2F2020%22%2C%22locfileid%22%3A%2291153529%22%2C%22product%22%3A%22dotnet-framework%22%2C%22technology%22%3A%22dotnet-data%22%2C%22pgtop%22%3A%22conceptual%22%2C%22translationtype%22%3A%22MT%22%2C%22giturl%22%3A%22https%3A%2F%2Fgithub.com%2Fdotnet%2Fdocs.zh-cn%2Fblob%2Flive-sxs%2Fdocs%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples.md%22%2C%22publishtime%22%3A%222020-09-24%2008%3A14%20PM%22%2C%22contentlocale%22%3A%22zh-cn%22%2C%22highContrast%22%3A%22false%22%2C%22metaTags%22%3A%7B%7D%7D%27&amp;-contentVer=%272.0%27&amp;-market=%27zh-cn%27&amp;-pageType=%27conceptual%27&amp;-actionType=%27O%27&amp;-behavior=300&amp;-content=%27%5B%7B%22event%22%3A%22page-focus-changed%22%2C%22value%22%3A%22blur%22%7D%5D%27&amp;*baseType=%27Ms.Content.PageAction%27&amp;*timeToAction=11244233&amp;*cookieEnabled=true&amp;*isJs=true&amp;*title=%27%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%20-%20ADO.NET%20%7C%20Microsoft%20Docs%27&amp;*isLoggedIn=false&amp;*isManual=true&amp;ext-javascript-ver=%271.1%27&amp;ext-javascript-libVer=%274.3.4%27&amp;ext-javascript-domain=%27docs.microsoft.com%27&amp;ext-javascript-msfpc=%27GUID%3Dbe533a6349374a19a3147b6350f4741a%26HASH%3Dbe53%26LV%3D202102%26V%3D4%26LU%3D1614099774713%27&amp;ext-javascript-userConsent=true&amp;ext-user-localId=%27t%3A01CDCD9B69DF681D0993C27C6DDF6E74%27&amp;$mscomCookies=false"></script></head>

<body lang="zh-cn" dir="ltr"><div id="MathJax_Message" style="display: none;"></div><div style="display: none;" id="lightningjs-usabilla_live"><div><iframe frameborder="0" id="lightningjs-frame-usabilla_live" src="./代码示例 - ADO.NET _ Microsoft Docs_files/saved_resource.html"></iframe></div></div>
	<div class="header-holder has-default-focus">
		<a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#main" class="skip-to-main-link has-outline-color-text visually-hidden-until-focused is-fixed has-inner-focus focus-visible has-top-zero has-left-zero has-right-zero has-padding-medium has-text-centered has-body-background-medium" tabindex="1">跳转至主内容</a>
		
		<div id="ignite-banner-holder" class="has-default-focus has-overflow-hidden"></div>
		
		<div hidden="" id="cookie-consent-holder"></div>
		<!-- liquid-tag banners global -->
		<div id="headerAreaHolder" data-bi-name="header">
<header role="banner" itemscope="itemscope" itemtype="http://schema.org/Organization"><!----> <div class="nav-bar"><!---->  <div class="nav-bar-brand">
		<a class="nav-bar-button" itemprop="url" href="https://www.microsoft.com/" aria-label="Microsoft">
			<div class="nav-bar-logo" role="presentation" aria-hidden="true" itemprop="logo" itemscope="itemscope">
				<svg xmlns="http://www.w3.org/2000/svg" width="108" height="23" viewBox="72 72 337 74" preserveAspectRatio="xMidYMin slice">
					<g data-name="MS-symbol">
						<clippath id="a">
							<path transform="matrix(1 0 0 -1 0 216)" d="M0 216h482V0H0z"></path>
						</clippath>
						<g clip-path="url(#a)">
							<path class="has-fill-current-color has-text-subtle" d="M394.942 104.59h-10.858v25.003h-7.38V104.59h-5.182v-5.965h5.182v-4.308c0-3.254 1.06-5.92 3.178-7.998 2.12-2.079 4.835-3.118 8.15-3.118.882 0 1.666.045 2.35.135a9.37 9.37 0 011.806.407v6.296c-.24-.14-.663-.31-1.265-.512-.603-.2-1.296-.3-2.078-.3-1.528 0-2.702.476-3.526 1.43-.824.954-1.235 2.365-1.235 4.232v3.736h10.858v-6.959l7.321-2.229v9.188h7.381v5.965h-7.38v14.49c0 1.91.345 3.254 1.038 4.037.693.783 1.782 1.175 3.27 1.175.42 0 .927-.1 1.52-.3a7.178 7.178 0 001.552-.724v6.025c-.463.261-1.23.502-2.305.723a15.742 15.742 0 01-3.178.331c-3.073 0-5.378-.817-6.914-2.455-1.537-1.637-2.305-4.102-2.305-7.396zm-48.407 9.73c0 3.233.733 5.703 2.2 7.411 1.465 1.707 3.564 2.56 6.295 2.56 2.652 0 4.67-.853 6.055-2.56 1.386-1.708 2.08-4.238 2.08-7.592 0-3.334-.719-5.849-2.155-7.547-1.436-1.697-3.45-2.545-6.04-2.545-2.67 0-4.745.888-6.22 2.666-1.477 1.777-2.215 4.313-2.215 7.607m-7.592.24c0-5.12 1.446-9.177 4.338-12.17 2.892-2.993 6.91-4.489 12.05-4.489 4.841 0 8.621 1.441 11.343 4.323 2.721 2.883 4.082 6.774 4.082 11.674 0 5.021-1.447 9.018-4.338 11.99-2.892 2.973-6.829 4.458-11.81 4.458-4.8 0-8.61-1.41-11.432-4.232-2.822-2.82-4.233-6.673-4.233-11.554m-16.417-7.802c0 1.045.331 1.863.994 2.456.662.592 2.128 1.34 4.398 2.243 2.912 1.166 4.956 2.476 6.131 3.932 1.175 1.456 1.762 3.22 1.762 5.287 0 2.912-1.12 5.252-3.359 7.02-2.24 1.767-5.267 2.65-9.083 2.65-1.285 0-2.706-.155-4.263-.467-1.556-.31-2.877-.707-3.96-1.19v-7.169a17.945 17.945 0 004.277 2.198c1.526.543 2.911.814 4.157.814 1.647 0 2.862-.23 3.645-.693.784-.46 1.175-1.235 1.175-2.319 0-1.005-.406-1.853-1.22-2.546-.813-.693-2.355-1.492-4.624-2.395-2.69-1.125-4.599-2.39-5.724-3.796-1.125-1.406-1.687-3.193-1.687-5.362 0-2.792 1.11-5.086 3.33-6.884 2.217-1.797 5.095-2.696 8.63-2.696 1.084 0 2.3.12 3.645.361 1.346.242 2.47.553 3.374.934v6.93c-.964-.644-2.089-1.195-3.374-1.658-1.286-.462-2.56-.693-3.826-.693-1.386 0-2.465.271-3.238.813-.774.543-1.16 1.286-1.16 2.23m-35.066 7.562c0 3.233.733 5.703 2.2 7.411 1.465 1.707 3.564 2.56 6.295 2.56 2.652 0 4.67-.853 6.055-2.56 1.386-1.708 2.08-4.238 2.08-7.592 0-3.334-.719-5.849-2.155-7.547-1.436-1.697-3.449-2.545-6.039-2.545-2.672 0-4.745.888-6.222 2.666-1.476 1.777-2.214 4.313-2.214 7.607m-7.592.24c0-5.12 1.446-9.177 4.338-12.17 2.893-2.993 6.91-4.489 12.051-4.489 4.84 0 8.621 1.441 11.342 4.323 2.721 2.883 4.082 6.774 4.082 11.674 0 5.021-1.446 9.018-4.338 11.99-2.892 2.973-6.828 4.458-11.809 4.458-4.8 0-8.61-1.41-11.433-4.232-2.822-2.82-4.233-6.673-4.233-11.554m-3.136-16.448c.582 0 1.105.041 1.567.121.462.08.853.18 1.175.301v7.38c-.382-.28-.939-.546-1.672-.798-.733-.25-1.622-.376-2.666-.376-1.788 0-3.299.753-4.534 2.26-1.235 1.505-1.853 3.825-1.853 6.958v15.635h-7.29V98.624h7.29v4.88h.12c.663-1.687 1.667-3.007 3.013-3.96 1.346-.954 2.962-1.432 4.85-1.432m-27.956 26.18c1.084 0 2.28-.25 3.585-.754a15.06 15.06 0 003.615-1.988v6.778c-1.165.663-2.485 1.165-3.962 1.506-1.475.342-3.098.512-4.865.512-4.559 0-8.265-1.44-11.116-4.323-2.852-2.88-4.278-6.562-4.278-11.04 0-4.98 1.456-9.083 4.37-12.307 2.91-3.224 7.037-4.835 12.38-4.835 1.366 0 2.746.176 4.143.527 1.395.352 2.504.758 3.328 1.22v6.99c-1.125-.824-2.274-1.462-3.45-1.914a9.966 9.966 0 00-3.599-.678c-2.872 0-5.192.934-6.959 2.802-1.768 1.868-2.652 4.388-2.652 7.562 0 3.132.85 5.573 2.546 7.32 1.697 1.748 4.002 2.621 6.914 2.621m-22.112 5.302h-7.29V98.624h7.29zm-8.044-39.916c0-1.205.437-2.214 1.311-3.028.874-.813 1.913-1.22 3.118-1.22 1.285 0 2.35.417 3.193 1.25.844.834 1.266 1.833 1.266 2.998 0 1.185-.432 2.18-1.296 2.982-.864.804-1.918 1.205-3.163 1.205-1.246 0-2.295-.406-3.148-1.219-.854-.815-1.28-1.803-1.28-2.968m-6.274-3.284v43.2h-7.5v-33.86h-.12l-13.408 33.86h-4.97l-13.738-33.86h-.09v33.86h-6.929v-43.2h10.755l12.412 32.024h.18l13.105-32.024z"></path>
							<path d="M106.214 106.214H71.996V71.996h34.218z" fill="#f25022"></path>
							<path d="M143.993 106.214h-34.218V71.996h34.218z" fill="#7fba00"></path>
							<path d="M106.214 143.993H71.996v-34.218h34.218z" fill="#00a4ef"></path>
							<path d="M143.993 143.993h-34.218v-34.218h34.218z" fill="#ffb900"></path>
						</g>
					</g>
				</svg>
			</div>
		</a>
	</div> 
				
		<div class="nav-bar-item is-hidden-tablet">
			<div class="nav-bar-menu">
				<button class="nav-bar-button is-title" title="全球导航" aria-expanded="false">
					<span class="nav-bar-menu-title"><!---->文档<!----></span>
					<div class="nav-bar-burger">
						<span></span>
						<span></span>
						<span></span>
						<span class="nav-bar-burger-label"><!---->全球导航<!----></span>
					</div>
				</button>
			</div>
		</div>
	
				
		<nav class="nav-bar-nav" role="navigation" aria-label="全球">
			<ul class="nav-bar-nav-list">
				
		<li class="nav-bar-item is-category">
			<a class="nav-bar-button is-title has-hover-underline" itemprop="url" href="https://docs.microsoft.com/zh-cn/">
				<span><!---->Docs<!----></span>
			</a>
		</li>
	
				<!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline is-active" href="https://docs.microsoft.com/zh-cn/documentation">
					<span><!---->文档<!----></span>
				</a>
		  </li><!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline " href="https://docs.microsoft.com/zh-cn/learn/">
					<span><!---->Learn<!----></span>
				</a>
		  </li><!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline " href="https://docs.microsoft.com/zh-cn/answers/products/">
					<span><!---->Q&amp;A<!----></span>
				</a>
		  </li><!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline " href="https://docs.microsoft.com/zh-cn/samples/browse/">
					<span><!---->代码示例<!----></span>
				</a>
		  </li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-96">
			<span><!---->更多<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-96" aria-label="更多">
			<!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button is-active" href="https://docs.microsoft.com/zh-cn/documentation"><!---->文档<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button " href="https://docs.microsoft.com/zh-cn/learn/"><!---->Learn<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button " href="https://docs.microsoft.com/zh-cn/answers/products/"><!---->Q&amp;A<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button " href="https://docs.microsoft.com/zh-cn/samples/browse/"><!---->代码示例<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
			</ul>
		</nav>
	
				<span class="nav-bar-spacer"></span>
				
		<div class="nav-bar-search " id="ax-97" hidden="">
			
		<form class="nav-bar-search-form" method="GET" name="nav-bar-search-form" role="search" id="nav-bar-search-form" aria-label="搜索" aria-expanded="false" action="https://docs.microsoft.com/zh-cn/search/">
			<div class="autocomplete" data-bi-name="autocomplete"><!----> <div class="control ">
				<input role="combobox" maxlength="100" aria-autocomplete="list" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="site-search-input" class="autocomplete-input input   is-small" type="search" name="terms" aria-expanded="false" aria-owns="ax-98-listbox" aria-activedescendant="" aria-label="搜索" placeholder="搜索">

				<span aria-hidden="true" class="icon is-small is-left" hidden="">
					<span class="has-text-primary docon docon-undefined"></span>
				</span>

				<span aria-hidden="true" class="autocomplete-loader loader has-text-primary " hidden=""></span>
			</div>

			<ul class="autocomplete-suggestions is-vertically-scrollable" role="listbox" id="ax-98-listbox" aria-label="site-search-input-suggestions" hidden="">
				
			</ul><!----></div>
			<!-- mobile safari will not dispatch submit event unless there's a submit button that is not display:none -->
			<button type="submit" class="visually-hidden" tabindex="-1" aria-hidden="true"></button>
			<input name="category" hidden="" value="">
			<input name="scope" hidden="" value=".NET">
			
			
		</form>
	
		</div>
		
					<div class="nav-bar-search">
						<!-- hidden when the width drops to tablet -->
						
		<form class="nav-bar-search-form" method="GET" name="nav-bar-search-form" role="search" id="nav-bar-search-form-desktop" aria-label="搜索" aria-expanded="false" action="https://docs.microsoft.com/zh-cn/search/">
			<div class="autocomplete" data-bi-name="autocomplete"><!----> <div class="control has-icons-left">
				<input role="combobox" maxlength="100" aria-autocomplete="list" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="ax-99" class="autocomplete-input input control has-icons-left  is-small" type="search" name="terms" aria-expanded="false" aria-owns="ax-100-listbox" aria-activedescendant="" aria-label="搜索" placeholder="搜索">

				<span aria-hidden="true" class="icon is-small is-left">
					<span class="has-text-primary docon docon-search"></span>
				</span>

				<span aria-hidden="true" class="autocomplete-loader loader has-text-primary " hidden=""></span>
			</div>

			<ul class="autocomplete-suggestions is-vertically-scrollable" role="listbox" id="ax-100-listbox" aria-label="ax-99-suggestions" hidden="">
				
			</ul><!----></div>
			<!-- mobile safari will not dispatch submit event unless there's a submit button that is not display:none -->
			<button type="submit" class="visually-hidden" tabindex="-1" aria-hidden="true"></button>
			<input name="category" hidden="" value="">
			<input name="scope" hidden="" value=".NET">
			
			
		</form>
	
					</div>
			  
		<div class="nav-bar-search " hidden="">
			<div class="nav-bar-search-form-expander">
				<button id="form-expander" class="nav-bar-button has-hover-underline " title="搜索" aria-expanded="false" aria-controls="ax-97">
					
		<span class="is-visually-hidden-mobile"><!---->搜索<!----></span>
		<span class="icon" aria-hidden="true">
			<span class="docon docon-search"></span>
		</span>
	
				</button>
			</div>
		</div>
	
				
		<div class="nav-bar-profile">
			
		<a class="docs-sign-in auth-status-determined not-authenticated" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#"> 登录 </a>
	 
		<div class="dropdown has-caret auth-status-determined authenticated">
			<button class="dropdown-trigger button-reset" aria-controls="user-menu" aria-expanded="false" aria-label="你的帐户">
				<figure class="image is-36x36">
					<img class="has-body-background-medium is-rounded" alt="" data-profile-property="avatarUrl">
				</figure>
			</button>
			<div class="dropdown-menu box has-flex-justify-content-start has-body-background" id="user-menu" role="menu">
				<div class="media is-user-dir">
					<div class="media-left">
						<figure class="image is-48x48" aria-hidden="true">
							<img class="has-body-background-medium is-rounded" aria-labelledby="displayName" data-profile-property="avatarUrl">
						</figure>
					</div>
					<div class="media-content">
						<p class="title is-size-large">
							<span id="displayName" class="is-inline-truncated has-max-width-200" data-profile-property="displayName"></span>
						</p>
						<p class="subtitle is-size-small has-margin-bottom-none">
							<span class="is-inline-truncated has-max-width-200" data-profile-property="upn"></span>
						</p>
					</div>
				</div>
				<ul class="has-padding-top-small has-padding-bottom-medium">
					<li class="has-padding-top-extra-small has-padding-bottom-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="profileUrl"><!---->配置文件<!----></a>
					</li>
					<li class="has-padding-top-extra-small has-padding-bottom-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="bookmarksUrl"><!---->书签<!----></a>
					</li>
					<li class="has-padding-top-extra-small has-padding-bottom-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="collectionsUrl"><!---->集合<!----></a>
					</li>
					<li class="has-padding-top-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="challengesUrl"><!---->挑战<!----></a>
					</li>
				</ul>
				<div class="has-border-top has-padding-top-medium">
					<a class="docs-sign-out" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#"><!---->注销<!----></a>
				</div>
			</div>
		</div>
	
		</div>
	
			 <!----></div> <div class="nav-bar has-border-top is-hidden-mobile"><!----> 
		<a class="nav-bar-button is-title has-hover-underline" itemprop="url" href="https://docs.microsoft.com/dotnet/">
			<span>
				.NET
				
			</span>
		</a>
	 
		
		<nav class="nav-bar-nav" role="navigation" aria-label="站点">
			<ul class="nav-bar-nav-list">
				
				<!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-101">
			<span><!---->语言<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-101" aria-label="语言">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/csharp/"><!---->C#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/fsharp/"><!---->F#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/visual-basic/"><!---->Visual Basic<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-102">
			<span><!---->工作负载<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-102" aria-label="工作负载">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/aspnet/core/"><!---->Web<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/xamarin/"><!---->移动<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/azure/"><!---->云<!----></a>
				</li>
		  <!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-103">
			<span><!---->桌面<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-103" aria-label="桌面">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/desktop-wpf/"><!---->Windows Presentation Foundation<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/"><!---->Windows 窗体<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/windows/uwp/"><!---->通用 Windows 应用<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-104">
			<span><!---->机器学习和数据<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-104" aria-label="机器学习和数据">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/machine-learning/"><!---->ML.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/spark/"><!---->.NET for Apache Spark<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/ef/"><!---->实体框架<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-105">
			<span><!---->API<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-105" aria-label="API">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netcore-3.1"><!---->.NET Core<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframework-4.8"><!---->.NET Framework<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=aspnetcore-3.1"><!---->ASP.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=ml-dotnet"><!---->ML.NET<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-106">
			<span><!---->资源<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-106" aria-label="资源">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/what-is-dotnet"><!---->什么是 .NET？<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/architecture-guides"><!---->.NET 体系结构指南<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn"><!---->学习材料<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/download"><!---->下载<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/community"><!---->社区<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/support"><!---->支持<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://devblogs.microsoft.com/dotnet/"><!---->博客<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-107">
			<span><!---->更多<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-107" aria-label="更多">
			<!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-108">
			<span><!---->语言<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-108" aria-label="语言">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/csharp/"><!---->C#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/fsharp/"><!---->F#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/visual-basic/"><!---->Visual Basic<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-109">
			<span><!---->工作负载<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-109" aria-label="工作负载">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/aspnet/core/"><!---->Web<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/xamarin/"><!---->移动<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/azure/"><!---->云<!----></a>
				</li>
		  <!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-110">
			<span><!---->桌面<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-110" aria-label="桌面">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/desktop-wpf/"><!---->Windows Presentation Foundation<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/"><!---->Windows 窗体<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/windows/uwp/"><!---->通用 Windows 应用<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-111">
			<span><!---->机器学习和数据<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-111" aria-label="机器学习和数据">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/machine-learning/"><!---->ML.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/spark/"><!---->.NET for Apache Spark<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/ef/"><!---->实体框架<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-112">
			<span><!---->API<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-112" aria-label="API">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netcore-3.1"><!---->.NET Core<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframework-4.8"><!---->.NET Framework<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=aspnetcore-3.1"><!---->ASP.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=ml-dotnet"><!---->ML.NET<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-113">
			<span><!---->资源<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-113" aria-label="资源">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/what-is-dotnet"><!---->什么是 .NET？<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/architecture-guides"><!---->.NET 体系结构指南<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn"><!---->学习材料<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/download"><!---->下载<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/community"><!---->社区<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/support"><!---->支持<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://devblogs.microsoft.com/dotnet/"><!---->博客<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
		</ul>
	</li><!---->
			</ul>
		</nav>
	
		<span class="nav-bar-spacer"></span>
		
		<ol class="nav-bar-list">
			 
		<li class="nav-bar-item ">
			<a class="button is-small is-primary " href="https://dotnet.microsoft.com/download">
				下载 .NET
			</a>
		</li>
	
		</ol>
	 
	 <!----></div> <!----></header>		</div>

						<div class="content-header uhf-container has-padding has-default-focus has-border-bottom-none" data-bi-name="content-header">
					<nav class="breadcrumb-holder has-padding-none has-padding-left-medium-tablet has-padding-right-medium-tablet has-padding-left-none-uhf-tablet has-padding-left-none-uhf-tablet has-padding-none-desktop has-flex-grow" data-bi-name="breadcrumb" itemscope="" itemtype="http://schema.org/BreadcrumbList" role="navigation" aria-label="痕迹">
						<ul id="page-breadcrumbs" class="breadcrumbs"><!----><!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/">
								<span itemprop="name"><!---->Docs<!----></span>
							</a>
					  
				<meta itemprop="position" content="1">
			</li>
		<!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/dotnet/">
								<span itemprop="name"><!---->.NET<!----></span>
							</a>
					  
				<meta itemprop="position" content="2">
			</li>
		<!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/dotnet/framework/">
								<span itemprop="name"><!---->.NET Framework<!----></span>
							</a>
					  
				<meta itemprop="position" content="3">
			</li>
		<!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/">
								<span itemprop="name"><!---->数据和建模<!----></span>
							</a>
					  
				<meta itemprop="position" content="4">
			</li>
		<!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/">
								<span itemprop="name"><!---->ADO.<wbr>NET<!----></span>
							</a>
					  
				<meta itemprop="position" content="5">
			</li>
		<!----><!----></ul>
					</nav>
				<div class="content-header-controls">
					<button type="button" class="contents-button button" data-bi-name="contents-expand" aria-haspopup="true">
						<span class="icon"><span class="docon docon-menu" aria-hidden="true"></span></span>
						<span class="contents-expand-title">目录</span>
					</button>
					<button type="button" class="ap-collapse-behavior ap-expanded button" data-bi-name="ap-collapse" aria-controls="action-panel">
						<span class="icon"><span class="docon docon-exit-mode" aria-hidden="true"></span></span>
						<span>退出焦点模式</span>
					</button>
				</div>
				<div class="has-padding-none-tablet has-padding-medium is-size-small is-flex-touch has-flex-justify-content-space-between-touch has-flex-grow page-action-holder">
					<ul class="is-hidden-mobile action-list has-flex-justify-content-start has-flex-justify-content-end-tablet is-flex is-flex-row has-flex-wrap has-flex-grow is-unstyled">
						<li>

<div class="language dropdown has-caret is-inline-block has-border-mobile">
	<button class="dropdown-trigger button is-text is-small" aria-controls="language-menu" aria-expanded="false" title="语言" data-bi-name="language">
		<span class="icon is-hidden-desktop has-margin-none">
			<span class="docon docon-code-lang" aria-hidden="true"></span>
		</span>
		<span class="lang-title is-visually-hidden-touch has-flex-align-self-start is-hidden-portrait" dir="ltr">C#</span>
		<span class="icon is-hidden-mobile is-hidden-tablet-only" aria-hidden="true">
			<span class="docon docon-chevron-down-light expanded-indicator"></span>
		</span>
	</button>
	<div class="dropdown-menu has-padding-small language-selector" name="language-selector" role="menu">
		<ul data-bi-name="language-links" dir="ltr">
			<li>
				<a class="button is-text is-small is-fullwidth has-flex-justify-content-start" data-bi-name="lang-csharp" tabindex="0" aria-selected="true">C#</a>
			</li>
			<li>
				<a class="button is-text is-small is-fullwidth has-flex-justify-content-start" data-bi-name="lang-vb" tabindex="0" aria-selected="false">VB</a>
			</li>
		</ul>
	</div>
</div>						</li>
						<li>
							<button type="button" class="bookmark button is-text has-inner-focus is-small is-icon-only-touch" data-list-type="bookmarks" data-bi-name="bookmark" title="添加 代码示例 - ADO.NET | Microsoft Docs 的书签" aria-pressed="false">
								<span class="icon" aria-hidden="true">
									<span class="docon docon-single-bookmark"></span>
								</span>
								<span class="bookmark-status is-visually-hidden-touch is-hidden-portrait">书签</span>
							</button>
						</li>
								<li id="contenteditbtn" hidden="">
									<a href="https://github.com/dotnet/docs.zh-cn/blob/live/docs/framework/data/adonet/ado-net-code-examples.md" class="button is-text has-inner-focus is-icon-only-touch is-small" title="编辑本文" data-bi-name="edit" data-original_content_git_url="https://github.com/dotnet/docs.zh-cn/blob/live-sxs/docs/framework/data/adonet/ado-net-code-examples.md" data-original_content_git_url_template="{repo}/blob/{branch}/docs/framework/data/adonet/ado-net-code-examples.md" data-pr_repo="" data-pr_branch="">
									<span class="icon" aria-hidden="true">
										<span class="docon docon-edit-outline"></span>
									</span>
									<span class="is-visually-hidden-touch is-hidden-portrait">编辑</span>
								</a>
							</li>
						<li>
<div class="sharing dropdown has-caret">
	<button class="dropdown-trigger button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small is-icon-only-touch" aria-controls="sharing-menu" aria-expanded="false" title="共享本文" data-bi-name="share">
		<span class="icon" aria-hidden="true">
			<span class="docon docon-sharing"></span>
		</span>
		<span class="is-visually-hidden-touch is-hidden-portrait">共享</span>
	</button>
	<div class="dropdown-menu has-padding-small" id="sharing-menu">
		<ul data-bi-name="share-links">
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-twitter" data-bi-name="twitter" href="https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples%3FWT.mc_id%3Dtwitter&amp;text=%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%20-%20ADO.NET%20%7C%20Microsoft%20Docs&amp;tw_p=tweetbutton&amp;url=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples%3FWT.mc_id%3Dtwitter">
					<span class="icon">
						<span class="docon docon-brand-twitter has-text-primary" aria-hidden="true"></span>
					</span>
					<span>Twitter</span>
				</a>
			</li>
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-linkedin" data-bi-name="linkedin" href="https://www.linkedin.com/cws/share?url=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples%3FWT.mc_id%3Dlinkedin">
					<span class="icon">
						<span class="docon docon-brand-linkedin has-text-primary" aria-hidden="true"></span>
					</span>
					<span>LinkedIn</span>
				</a>
			</li>
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-facebook" data-bi-name="facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples%3FWT.mc_id%3Dfacebook">
					<span class="icon">
						<span class="docon docon-brand-facebook has-text-primary" aria-hidden="true"></span>
					</span>
					<span>Facebook</span>
				</a>
			</li>
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-email" data-bi-name="email" href="mailto:?subject=%5B%E5%85%B1%E4%BA%AB%E6%96%87%E7%AB%A0%5D%20%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%20-%20ADO.NET%20%7C%20Microsoft%20Docs&amp;body=%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%20-%20ADO.NET%20%7C%20Microsoft%20Docs%0A%0Ahttps%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fado-net-code-examples%3FWT.mc_id%3Demail">
					<span class="icon">
						<span class="docon docon-mail-message-fill has-text-primary" aria-hidden="true"></span>
					</span>
					<span>电子邮件</span>
				</a>
			</li>
			<li>
				<span class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-wechat has-padding-top-extra-small has-padding-left-medium has-padding-right-medium">
					<span class="icon">
						<span class="docon docon-wechat has-text-primary" data-bi-name="wechat" aria-hidden="true"></span>
					</span>
					<span>微信</span>
				</span>
			</li>
			<li>
				<canvas class="qr-code" height="96" width="96"></canvas>
			</li>
		</ul>
	</div>
</div>						</li>
						<li>

<div class="lang-toggle-container" style="display: flex;">
	<label for="language-toggle" aria-hidden="true" tabindex="-1">使用英语阅读</label>
	<div class="checkbox-toggle-wrapper">
		<input type="checkbox" id="language-toggle" aria-label="使用英语阅读" data-m="{&quot;cN&quot;:&quot;language-toggle&quot;,&quot;value&quot;:&quot;off&quot;}">
		<label for="language-toggle" class="switch"></label>
	</div>
</div>						</li>
					</ul>
					<button type="button" class="has-border contents-button button is-small is-text is-hidden-tablet has-inner-focus" aria-label="目录" data-bi-name="contents-expand">
						<span class="icon">
							<span class="docon docon-editor-list-bullet" aria-hidden="true"></span>
						</span>
						<span class="contents-expand-title">目录</span>
					</button>
					<div class="is-invisible"></div>
					<div class="is-hidden-tablet level-item is-flexible level-right">

<div class="language dropdown has-caret is-inline-block has-border-mobile">
	<button class="dropdown-trigger button is-text is-small" aria-controls="language-menu" aria-expanded="false" title="语言" data-bi-name="language">
		<span class="icon is-hidden-desktop has-margin-none">
			<span class="docon docon-code-lang" aria-hidden="true"></span>
		</span>
		<span class="lang-title is-visually-hidden-touch has-flex-align-self-start is-hidden-portrait" dir="ltr">C#</span>
		<span class="icon is-hidden-mobile is-hidden-tablet-only" aria-hidden="true">
			<span class="docon docon-chevron-down-light expanded-indicator"></span>
		</span>
	</button>
	<div class="dropdown-menu has-padding-small language-selector" name="language-selector" role="menu">
		<ul data-bi-name="language-links" dir="ltr">
			<li>
				<a class="button is-text is-small is-fullwidth has-flex-justify-content-start" data-bi-name="lang-csharp" tabindex="0" aria-selected="false">C#</a>
			</li>
			<li>
				<a class="button is-text is-small is-fullwidth has-flex-justify-content-start" data-bi-name="lang-vb" tabindex="0" aria-selected="false">VB</a>
			</li>
		</ul>
	</div>
</div>						<button type="button" class="page-actions-button button is-small is-text is-hidden-tablet has-inner-focus has-border is-full-height  has-margin-left-small" aria-label="页面操作" data-bi-name="pageactions">
							<span class="icon">
								<span class="docon docon-more-vertical" aria-hidden="true"></span>
							</span>
						</button>
					</div>
				</div>
			</div>



		<div id="disclaimer-holder" class="has-overflow-hidden has-default-focus"><!----> <section data-dismissable="disappearing" class="is-info alert is-banner">
			<div class="uhf-container">
				<button data-dismiss="" type="button" class="delete is-absolute has-top-zero has-right-zero has-margin-extra-small is-large">
					<span class="visually-hidden"><!---->消除警报<!----></span>
				</button>
				<div class="columns">
					<div class="column is-12">
						<div class="level">
							<div class="level-left has-flex-shrink">
								<div class="level-item has-flex-shrink">
									<p class="has-text-white-static disclaimer-title has-margin-top-extra-small has-margin-right-medium has-line-height-reset">
										<span class="icon">
											<span class="docon docon-status-error-outline" aria-hidden="true"></span>
										</span>
										<span><!---->本主题的部分内容可能是由机器翻译。<!----></span>
									</p>
								</div>
							</div>
							
						</div>
					</div>
				</div>
			</div>
		</section><!----></div>
	</div>

	<div class="mainContainer  uhf-container has-top-padding  has-default-focus" data-bi-name="body">

		<div class="columns has-large-gaps is-gapless-mobile ">

			<div id="left-container" class="left-container is-hidden-mobile column is-one-third-tablet is-one-quarter-desktop">
				<nav id="affixed-left-container" class="is-fixed is-flex is-flex-column" role="navigation" aria-label="主要" style="width: 320.25px; top: 24px; bottom: 24px;" data-bi-name="left toc"><form action="javascript:" role="search" aria-label="搜索" class="has-margin-bottom-small"><label class="visually-hidden" for="ax-114">搜索</label><div class="autocomplete is-block" data-bi-name="autocomplete"><!----> <div class="control has-icons-left">
				<input role="combobox" maxlength="100" aria-autocomplete="list" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="ax-114" class="autocomplete-input input control has-icons-left is-full-width is-small" type="text" aria-expanded="false" aria-owns="ax-115-listbox" aria-activedescendant="" placeholder="按标题筛选">

				<span aria-hidden="true" class="icon is-small is-left">
					<span class="has-text-primary docon docon-filter-settings"></span>
				</span>

				<span aria-hidden="true" class="autocomplete-loader loader has-text-primary " hidden=""></span>
			</div>

			<ul class="autocomplete-suggestions is-vertically-scrollable" role="listbox" id="ax-115-listbox" aria-label="ax-114-suggestions" hidden="">
				
			</ul><!----></div></form><ul class="tree is-vertically-scrollable has-flex-grow has-flex-shrink" role="tree" aria-label="目录" data-bi-name="tree" data-is-collapsible="true"><li role="none"><a aria-setsize="11" aria-level="1" aria-posinset="1" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/">ADO.<wbr>NET</a></li><li role="none"><a aria-setsize="11" aria-level="1" aria-posinset="2" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/whats-new">ADO.<wbr>NET 新增功能</a></li><li class="tree-item is-expanded" aria-setsize="11" aria-level="1" aria-posinset="3" role="treeitem" tabindex="-1" id="title-3-1" aria-expanded="true"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>ADO.<wbr>NET 概述</span><ul class="tree-group" role="group"><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="1" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-overview">ADO.<wbr>NET 概述</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="2" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-architecture">ADO.<wbr>NET 体系结构</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="3" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-technology-options-and-guidelines">ADO.<wbr>NET 技术选项和准则</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="4" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/linq-and-ado-net">LINQ 和 ADO.<wbr>NET</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="5" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/data-providers">.NET Framework 数据提供程序</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="6" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-datasets">ADO.<wbr>NET 数据集</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="7" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/side-by-side-execution">ADO.<wbr>NET 中的并行执行</a></li><li role="none"><a aria-setsize="8" aria-level="2" aria-posinset="8" role="treeitem" tabindex="0" class="tree-item is-leaf has-external-link-indicator is-selected" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples" aria-current="page">ADO.<wbr>NET 代码示例</a></li></ul></li><li class="tree-item" aria-setsize="11" aria-level="1" aria-posinset="4" role="treeitem" tabindex="-1" id="title-4-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>保证 ADO.<wbr>NET 应用程序的安全</span></li><li class="tree-item" aria-setsize="11" aria-level="1" aria-posinset="5" role="treeitem" tabindex="-1" id="title-5-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>ADO.<wbr>NET 中的数据类型映射</span></li><li class="tree-item" aria-setsize="11" aria-level="1" aria-posinset="6" role="treeitem" tabindex="-1" id="title-6-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>在 ADO.<wbr>NET 中检索和修改数据</span></li><li class="tree-item" aria-setsize="11" aria-level="1" aria-posinset="7" role="treeitem" tabindex="-1" id="title-7-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>实体数据模型</span></li><li class="tree-item" aria-setsize="11" aria-level="1" aria-posinset="8" role="treeitem" tabindex="-1" id="title-8-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>Oracle 和 ADO.<wbr>NET</span></li><li role="none"><a aria-setsize="11" aria-level="1" aria-posinset="9" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/">ADO.<wbr>NET 实体框架</a></li><li role="none"><a aria-setsize="11" aria-level="1" aria-posinset="10" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/sql/">SQL Server 和 ADO.<wbr>NET</a></li><li role="none"><a aria-setsize="11" aria-level="1" aria-posinset="11" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/dataset-datatable-dataview/">数据集、数据表和数据视图</a></li></ul><a class="button is-small is-text has-flex-justify-content-start has-inner-focus has-margin-top-small has-border-top has-border-bottom has-flex-shrink-none" href="https://docs.microsoft.com/zh-cn/dotnet/opbuildpdf/framework/data/adonet/toc.pdf?branch=live" data-bi-name="downloadPdf" rel="nofollow"><!----> <span class="icon is-size-extra-small has-text-subtle">
				<span aria-hidden="true" class="docon docon-dictionary-download"></span>
			</span>
			<span><!---->下载 PDF<!----></span><!----></a></nav>
			</div>

			<section class="primary-holder column is-two-thirds-tablet is-three-quarters-desktop">
				<div class="columns is-gapless-mobile has-large-gaps ">


				<div id="main-column" class="column  is-full is-four-fifths-desktop ">

					<main id="main" role="main" class="content " data-bi-name="content" lang="zh-cn" dir="ltr">



						<h1 id="adonet-code-examples"><span data-ttu-id="a3bf0-103">ADO.NET 代码示例</span><span class="sxs-lookup"><span data-stu-id="a3bf0-103">ADO.NET code examples</span></span></h1>

						<ul class="metadata page-metadata" data-bi-name="page info" lang="zh-cn" dir="ltr">
							<li>
								<time class="" data-article-date="" aria-label="文章评审日期" datetime="2017-03-30T00:00:00.000Z" data-article-date-source="ms.date">2017/03/30</time>
							</li>
								<li class="contributors-holder">
									<ul class="facepile has-margin-left-none is-small" data-bi-name="contributors">
														<li class="facepile-item">
															<a href="https://github.com/stevestein" title="stevestein" aria-label="stevesteinGithub 个人资料" data-src="https://github.com/stevestein.png?size=32" data-contributor-name="stevestein" class="facepile-item-coin">
																<img class="facepile-item-coin-image durable-image" aria-hidden="true" src="https://github.com/stevestein.png?size=32" role="presentation" onerror="this.className=&#39;facepile-item-coin-image durable-image has-error&#39;;">
																<span class="facepile-item-coin-text durable-image-fallback facepile-item-teal" aria-hidden="true">s</span>
															</a>
														</li>
														<li class="facepile-item">
															<a href="https://github.com/olprod" title="olprod" aria-label="olprodGithub 个人资料" data-src="https://github.com/olprod.png?size=32" data-contributor-name="olprod" class="facepile-item-coin">
																<img class="facepile-item-coin-image durable-image" aria-hidden="true" src="./代码示例 - ADO.NET _ Microsoft Docs_files/olprod.png" role="presentation" onerror="this.className=&#39;facepile-item-coin-image durable-image has-error&#39;;">
																<span class="facepile-item-coin-text durable-image-fallback facepile-item-red" aria-hidden="true">o</span>
															</a>
														</li>
														<li class="facepile-item">
															<a href="https://github.com/Saisang" title="Saisang" aria-label="SaisangGithub 个人资料" data-src="https://github.com/Saisang.png?size=32" data-contributor-name="Saisang" class="facepile-item-coin">
																<img class="facepile-item-coin-image durable-image has-error" aria-hidden="true" src="https://github.com/Saisang.png?size=32" role="presentation" onerror="this.className=&#39;facepile-item-coin-image durable-image has-error&#39;;">
																<span class="facepile-item-coin-text durable-image-fallback facepile-item-blue" aria-hidden="true">S</span>
															</a>
														</li>
									</ul>
								</li>
						</ul>

						<nav id="center-doc-outline" class="doc-outline is-hidden-desktop" data-bi-name="intopic toc" role="navigation" aria-label="文章大纲">
							<h3>本文内容</h3>
						<ol><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#adonet-data-provider-examples">ADO.NET 数据提供程序示例</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#entity-framework-examples">实体框架示例</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#linq-to-sql">LINQ to SQL</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#see-also">请参阅</a></li></ol></nav>

						<!-- <content> -->
							<p><span data-ttu-id="a3bf0-104">本页上的代码列表演示如何使用以下 ADO.NET 技术从数据库中检索数据：</span><span class="sxs-lookup"><span data-stu-id="a3bf0-104">The code listings on this page demonstrate how to retrieve data from a database by using the following ADO.NET technologies:</span></span></p>
<ul>
<li><p><span data-ttu-id="a3bf0-105">ADO.NET 数据提供程序：</span><span class="sxs-lookup"><span data-stu-id="a3bf0-105">ADO.NET data providers:</span></span></p>
<ul>
<li><p><span data-ttu-id="a3bf0-106"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#sqlclient" data-linktype="self-bookmark">SqlClient</a> (<code>System.Data.SqlClient</code>) </span><span class="sxs-lookup"><span data-stu-id="a3bf0-106"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#sqlclient" data-linktype="self-bookmark">SqlClient</a> (<code>System.Data.SqlClient</code>)</span></span></p>
</li>
<li><p><span data-ttu-id="a3bf0-107"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#oledb" data-linktype="self-bookmark">OleDb</a> (<code>System.Data.OleDb</code>) </span><span class="sxs-lookup"><span data-stu-id="a3bf0-107"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#oledb" data-linktype="self-bookmark">OleDb</a> (<code>System.Data.OleDb</code>)</span></span></p>
</li>
<li><p><span data-ttu-id="a3bf0-108"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#odbc" data-linktype="self-bookmark">Odbc</a> (<code>System.Data.Odbc</code>) </span><span class="sxs-lookup"><span data-stu-id="a3bf0-108"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#odbc" data-linktype="self-bookmark">Odbc</a> (<code>System.Data.Odbc</code>)</span></span></p>
</li>
<li><p><span data-ttu-id="a3bf0-109"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#oracleclient" data-linktype="self-bookmark">System.data.oracleclient</a> (<code>System.Data.OracleClient</code>) </span><span class="sxs-lookup"><span data-stu-id="a3bf0-109"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#oracleclient" data-linktype="self-bookmark">OracleClient</a> (<code>System.Data.OracleClient</code>)</span></span></p>
</li>
</ul>
</li>
<li><p><span data-ttu-id="a3bf0-110">ADO.NET 实体框架：</span><span class="sxs-lookup"><span data-stu-id="a3bf0-110">ADO.NET Entity Framework:</span></span></p>
<ul>
<li><p><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#linq-to-entities" data-linktype="self-bookmark"><span data-ttu-id="a3bf0-111">LINQ to Entities</span><span class="sxs-lookup"><span data-stu-id="a3bf0-111">LINQ to Entities</span></span></a></p>
</li>
<li><p><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#typed-objectquery" data-linktype="self-bookmark"><span data-ttu-id="a3bf0-112">类型化 ObjectQuery</span><span class="sxs-lookup"><span data-stu-id="a3bf0-112">Typed ObjectQuery</span></span></a></p>
</li>
<li><p><span data-ttu-id="a3bf0-113"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#entityclient" data-linktype="self-bookmark">EntityClient</a> (<code>System.Data.EntityClient</code>) </span><span class="sxs-lookup"><span data-stu-id="a3bf0-113"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#entityclient" data-linktype="self-bookmark">EntityClient</a> (<code>System.Data.EntityClient</code>)</span></span></p>
</li>
</ul>
</li>
<li><p><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#linq-to-sql" data-linktype="self-bookmark"><span data-ttu-id="a3bf0-114">LINQ to SQL</span><span class="sxs-lookup"><span data-stu-id="a3bf0-114">LINQ to SQL</span></span></a></p>
</li>
</ul>
<h2 id="adonet-data-provider-examples" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#adonet-data-provider-examples" aria-labelledby="adonet-data-provider-examples"></a><span data-ttu-id="a3bf0-115">ADO.NET 数据提供程序示例</span><span class="sxs-lookup"><span data-stu-id="a3bf0-115">ADO.NET data provider examples</span></span></h2>
<p><span data-ttu-id="a3bf0-116">以下代码列表演示如何使用 ADO.NET 数据提供程序从数据库中检索数据。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-116">The following code listings demonstrate how to retrieve data from a database using ADO.NET data providers.</span></span> <span data-ttu-id="a3bf0-117">数据在一个 <code>DataReader</code> 中返回。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-117">The data is returned in a <code>DataReader</code>.</span></span> <span data-ttu-id="a3bf0-118">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/retrieving-data-using-a-datareader" data-linktype="relative-path">使用 DataReader 检索数据</a>。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-118">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/retrieving-data-using-a-datareader" data-linktype="relative-path">Retrieving Data Using a DataReader</a>.</span></span></p>
<h3 id="sqlclient" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#sqlclient" aria-labelledby="sqlclient"></a><span data-ttu-id="a3bf0-119">SqlClient</span><span class="sxs-lookup"><span data-stu-id="a3bf0-119">SqlClient</span></span></h3>
<p><span data-ttu-id="a3bf0-120">本示例中的代码假定您可以连接到 <code>Northwind</code> Microsoft SQL Server 上的示例数据库。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-120">The code in this example assumes that you can connect to the <code>Northwind</code> sample database on Microsoft SQL Server.</span></span> <span data-ttu-id="a3bf0-121">在此情形 5 中，示例代码创建一个 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlcommand" data-linktype="absolute-path">SqlCommand</a> 以从 Products 表中选择行，并添加 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlparameter" data-linktype="absolute-path">SqlParameter</a> 来将结果限制为其 UnitPrice 大于指定参数值的行。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-121">The code creates a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlcommand" data-linktype="absolute-path">SqlCommand</a> to select rows from the Products table, adding a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlparameter" data-linktype="absolute-path">SqlParameter</a> to restrict the results to rows with a UnitPrice greater than the specified parameter value, in this case 5.</span></span> <span data-ttu-id="a3bf0-122">在 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlconnection" data-linktype="absolute-path">SqlConnection</a> 块中打开 <code>using</code> ，这可确保在代码退出时关闭并释放资源。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-122">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlconnection" data-linktype="absolute-path">SqlConnection</a> is opened inside a <code>using</code> block, which ensures that resources are closed and disposed when the code exits.</span></span> <span data-ttu-id="a3bf0-123">示例代码使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqldatareader" data-linktype="absolute-path">SqlDataReader</a> 执行命令，并在控制台窗口中显示结果。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-123">The code executes the command by using a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqldatareader" data-linktype="absolute-path">SqlDataReader</a>, and displays the results in the console window.</span></span></p>
<div class="codeHeader" id="code-try-0" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" name="DataWorks SampleApp.SqlClient#1" data-author-content="using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString =
            &quot;Data Source=(local);Initial Catalog=Northwind;&quot;
            + &quot;Integrated Security=true&quot;;

        // Provide the query string with a parameter placeholder.
        string queryString =
            &quot;SELECT ProductID, UnitPrice, ProductName from dbo.products &quot;
                + &quot;WHERE UnitPrice &gt; @pricePoint &quot;
                + &quot;ORDER BY UnitPrice DESC;&quot;;

        // Specify the parameter value.
        int paramValue = 5;

        // Create and open the connection in a using block. This
        // ensures that all resources will be closed and disposed
        // when the code exits.
        using (SqlConnection connection =
            new SqlConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue(&quot;@pricePoint&quot;, paramValue);

            // Open the connection in a try/catch block.
            // Create and execute the DataReader, writing the result
            // set to the console window.
            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(&quot;\t{0}\t{1}\t{2}&quot;,
                        reader[0], reader[1], reader[2]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Data;
<span class="hljs-keyword">using</span> System.Data.SqlClient;

<span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
{
    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">string</span> connectionString =
            <span class="hljs-string">"Data Source=(local);Initial Catalog=Northwind;"</span>
            + <span class="hljs-string">"Integrated Security=true"</span>;

        <span class="hljs-comment">// Provide the query string with a parameter placeholder.</span>
        <span class="hljs-keyword">string</span> queryString =
            <span class="hljs-string">"SELECT ProductID, UnitPrice, ProductName from dbo.products "</span>
                + <span class="hljs-string">"WHERE UnitPrice &gt; @pricePoint "</span>
                + <span class="hljs-string">"ORDER BY UnitPrice DESC;"</span>;

        <span class="hljs-comment">// Specify the parameter value.</span>
        <span class="hljs-keyword">int</span> paramValue = <span class="hljs-number">5</span>;

        <span class="hljs-comment">// Create and open the connection in a using block. This</span>
        <span class="hljs-comment">// ensures that all resources will be closed and disposed</span>
        <span class="hljs-comment">// when the code exits.</span>
        <span class="hljs-keyword">using</span> (SqlConnection connection =
            <span class="hljs-keyword">new</span> SqlConnection(connectionString))
        {
            <span class="hljs-comment">// Create the Command and Parameter objects.</span>
            SqlCommand command = <span class="hljs-keyword">new</span> SqlCommand(queryString, connection);
            command.Parameters.AddWithValue(<span class="hljs-string">"@pricePoint"</span>, paramValue);

            <span class="hljs-comment">// Open the connection in a try/catch block.</span>
            <span class="hljs-comment">// Create and execute the DataReader, writing the result</span>
            <span class="hljs-comment">// set to the console window.</span>
            <span class="hljs-keyword">try</span>
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                <span class="hljs-keyword">while</span> (reader.Read())
                {
                    Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}\t{2}"</span>,
                        reader[<span class="hljs-number">0</span>], reader[<span class="hljs-number">1</span>], reader[<span class="hljs-number">2</span>]);
                }
                reader.Close();
            }
            <span class="hljs-keyword">catch</span> (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
</span></code></pre><pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" name="DataWorks SampleApp.SqlClient#1" data-author-content="Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Public Class Program
    Public Shared Sub Main()

        Dim connectionString As String = _
            &quot;Data Source=(local);Initial Catalog=Northwind;&quot; _
            &amp; &quot;Integrated Security=true&quot;

        &#39; Provide the query string with a parameter placeholder.
        Dim queryString As String = _
            &quot;SELECT ProductID, UnitPrice, ProductName from dbo.Products &quot; _
            &amp; &quot;WHERE UnitPrice &gt; @pricePoint &quot; _
            &amp; &quot;ORDER BY UnitPrice DESC;&quot;

        &#39; Specify the parameter value.
        Dim paramValue As Integer = 5

        &#39; Create and open the connection in a using block. This
        &#39; ensures that all resources will be closed and disposed
        &#39; when the code exits.
        Using connection As New SqlConnection(connectionString)

            &#39; Create the Command and Parameter objects.
            Dim command As New SqlCommand(queryString, connection)
            command.Parameters.AddWithValue(&quot;@pricePoint&quot;, paramValue)

            &#39; Open the connection in a try/catch block. 
            &#39; Create and execute the DataReader, writing the result
            &#39; set to the console window.
            Try
                connection.Open()
                Dim dataReader As SqlDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine( _
                        vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot; &amp; vbTab &amp; &quot;{2}&quot;, _
                     dataReader(0), dataReader(1), dataReader(2))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.ReadLine()
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Public Class Program
    Public Shared Sub Main()

        Dim connectionString As String = _
            "Data Source=(local);Initial Catalog=Northwind;" _
            &amp; "Integrated Security=true"

        ' Provide the query string with a parameter placeholder.
        Dim queryString As String = _
            "SELECT ProductID, UnitPrice, ProductName from dbo.Products " _
            &amp; "WHERE UnitPrice &gt; @pricePoint " _
            &amp; "ORDER BY UnitPrice DESC;"

        ' Specify the parameter value.
        Dim paramValue As Integer = 5

        ' Create and open the connection in a using block. This
        ' ensures that all resources will be closed and disposed
        ' when the code exits.
        Using connection As New SqlConnection(connectionString)

            ' Create the Command and Parameter objects.
            Dim command As New SqlCommand(queryString, connection)
            command.Parameters.AddWithValue("@pricePoint", paramValue)

            ' Open the connection in a try/catch block. 
            ' Create and execute the DataReader, writing the result
            ' set to the console window.
            Try
                connection.Open()
                Dim dataReader As SqlDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine( _
                        vbTab &amp; "{0}" &amp; vbTab &amp; "{1}" &amp; vbTab &amp; "{2}", _
                     dataReader(0), dataReader(1), dataReader(2))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.ReadLine()
        End Using
    End Sub
End Class
</code></pre><h3 id="oledb" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#oledb" aria-labelledby="oledb"></a><span data-ttu-id="a3bf0-124">OleDb</span><span class="sxs-lookup"><span data-stu-id="a3bf0-124">OleDb</span></span></h3>
<p><span data-ttu-id="a3bf0-125">此示例中的代码假定你可以连接到 Microsoft Access Northwind 示例数据库。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-125">The code in this example assumes that you can connect to the Microsoft Access Northwind sample database.</span></span> <span data-ttu-id="a3bf0-126">在此情形 5 中，示例代码创建一个 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbcommand" data-linktype="absolute-path">OleDbCommand</a> 以从 Products 表中选择行，并添加 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbparameter" data-linktype="absolute-path">OleDbParameter</a> 来将结果限制为其 UnitPrice 大于指定参数值的行。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-126">The code creates a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbcommand" data-linktype="absolute-path">OleDbCommand</a> to select rows from the Products table, adding a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbparameter" data-linktype="absolute-path">OleDbParameter</a> to restrict the results to rows with a UnitPrice greater than the specified parameter value, in this case 5.</span></span> <span data-ttu-id="a3bf0-127"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbconnection" data-linktype="absolute-path">OleDbConnection</a> 在 <code>using</code> 块内打开，这将确保在代码退出时会关闭和释放资源。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-127">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbconnection" data-linktype="absolute-path">OleDbConnection</a> is opened inside of a <code>using</code> block, which ensures that resources are closed and disposed when the code exits.</span></span> <span data-ttu-id="a3bf0-128">示例代码使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbdatareader" data-linktype="absolute-path">OleDbDataReader</a> 执行命令，并在控制台窗口中显示结果。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-128">The code executes the command by using a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oledb.oledbdatareader" data-linktype="absolute-path">OleDbDataReader</a>, and displays the results in the console window.</span></span></p>
<div class="codeHeader" id="code-try-1" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" name="DataWorks SampleApp.OleDb#1" data-author-content="using System;
using System.Data;
using System.Data.OleDb;

class Program
{
    static void Main()
    {
        // The connection string assumes that the Access
        // Northwind.mdb is located in the c:\Data folder.
        string connectionString =
        &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;
            + &quot;c:\\Data\\Northwind.mdb;User Id=admin;Password=;&quot;;

        // Provide the query string with a parameter placeholder.
        string queryString =
            &quot;SELECT ProductID, UnitPrice, ProductName from products &quot;
                + &quot;WHERE UnitPrice &gt; ? &quot;
                + &quot;ORDER BY UnitPrice DESC;&quot;;

        // Specify the parameter value.
        int paramValue = 5;

        // Create and open the connection in a using block. This
        // ensures that all resources will be closed and disposed
        // when the code exits.
        using (OleDbConnection connection =
            new OleDbConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            OleDbCommand command = new OleDbCommand(queryString, connection);
            command.Parameters.AddWithValue(&quot;@pricePoint&quot;, paramValue);

            // Open the connection in a try/catch block.
            // Create and execute the DataReader, writing the result
            // set to the console window.
            try
            {
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(&quot;\t{0}\t{1}\t{2}&quot;,
                        reader[0], reader[1], reader[2]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Data;
<span class="hljs-keyword">using</span> System.Data.OleDb;

<span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
{
    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-comment">// The connection string assumes that the Access</span>
        <span class="hljs-comment">// Northwind.mdb is located in the c:\Data folder.</span>
        <span class="hljs-keyword">string</span> connectionString =
        <span class="hljs-string">"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="</span>
            + <span class="hljs-string">"c:\\Data\\Northwind.mdb;User Id=admin;Password=;"</span>;

        <span class="hljs-comment">// Provide the query string with a parameter placeholder.</span>
        <span class="hljs-keyword">string</span> queryString =
            <span class="hljs-string">"SELECT ProductID, UnitPrice, ProductName from products "</span>
                + <span class="hljs-string">"WHERE UnitPrice &gt; ? "</span>
                + <span class="hljs-string">"ORDER BY UnitPrice DESC;"</span>;

        <span class="hljs-comment">// Specify the parameter value.</span>
        <span class="hljs-keyword">int</span> paramValue = <span class="hljs-number">5</span>;

        <span class="hljs-comment">// Create and open the connection in a using block. This</span>
        <span class="hljs-comment">// ensures that all resources will be closed and disposed</span>
        <span class="hljs-comment">// when the code exits.</span>
        <span class="hljs-keyword">using</span> (OleDbConnection connection =
            <span class="hljs-keyword">new</span> OleDbConnection(connectionString))
        {
            <span class="hljs-comment">// Create the Command and Parameter objects.</span>
            OleDbCommand command = <span class="hljs-keyword">new</span> OleDbCommand(queryString, connection);
            command.Parameters.AddWithValue(<span class="hljs-string">"@pricePoint"</span>, paramValue);

            <span class="hljs-comment">// Open the connection in a try/catch block.</span>
            <span class="hljs-comment">// Create and execute the DataReader, writing the result</span>
            <span class="hljs-comment">// set to the console window.</span>
            <span class="hljs-keyword">try</span>
            {
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();
                <span class="hljs-keyword">while</span> (reader.Read())
                {
                    Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}\t{2}"</span>,
                        reader[<span class="hljs-number">0</span>], reader[<span class="hljs-number">1</span>], reader[<span class="hljs-number">2</span>]);
                }
                reader.Close();
            }
            <span class="hljs-keyword">catch</span> (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
</span></code></pre><pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" name="DataWorks SampleApp.OleDb#1" data-author-content="Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.OleDb

Public Class Program
    Public Shared Sub Main()

        &#39; The connection string assumes that the Access 
        &#39; Northwind.mdb is located in the c:\Data folder.
        Dim connectionString As String = _
             &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; _
            &amp; &quot;c:\Data\Northwind.mdb;User Id=admin;Password=;&quot;

        &#39; Provide the query string with a parameter placeholder.
        Dim queryString As String = _
            &quot;SELECT ProductID, UnitPrice, ProductName from Products &quot; _
            &amp; &quot;WHERE UnitPrice &gt; ? &quot; _
            &amp; &quot;ORDER BY UnitPrice DESC;&quot;

        &#39; Specify the parameter value.
        Dim paramValue As Integer = 5

        &#39; Create and open the connection in a using block. This
        &#39; ensures that all resources will be closed and disposed
        &#39; when the code exits.
        Using connection As New OleDbConnection(connectionString)

            &#39; Create the Command and Parameter objects.
            Dim command As New OleDbCommand(queryString, connection)
            command.Parameters.AddWithValue(&quot;@pricePoint&quot;, paramValue)

            &#39; Open the connection in a try/catch block. 
            &#39; Create and execute the DataReader, writing the result
            &#39; set to the console window.
            Try
                connection.Open()
                Dim dataReader As OleDbDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine( _
                        vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot; &amp; vbTab &amp; &quot;{2}&quot;, _
                     dataReader(0), dataReader(1), dataReader(2))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.ReadLine()
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.OleDb

Public Class Program
    Public Shared Sub Main()

        ' The connection string assumes that the Access 
        ' Northwind.mdb is located in the c:\Data folder.
        Dim connectionString As String = _
             "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
            &amp; "c:\Data\Northwind.mdb;User Id=admin;Password=;"

        ' Provide the query string with a parameter placeholder.
        Dim queryString As String = _
            "SELECT ProductID, UnitPrice, ProductName from Products " _
            &amp; "WHERE UnitPrice &gt; ? " _
            &amp; "ORDER BY UnitPrice DESC;"

        ' Specify the parameter value.
        Dim paramValue As Integer = 5

        ' Create and open the connection in a using block. This
        ' ensures that all resources will be closed and disposed
        ' when the code exits.
        Using connection As New OleDbConnection(connectionString)

            ' Create the Command and Parameter objects.
            Dim command As New OleDbCommand(queryString, connection)
            command.Parameters.AddWithValue("@pricePoint", paramValue)

            ' Open the connection in a try/catch block. 
            ' Create and execute the DataReader, writing the result
            ' set to the console window.
            Try
                connection.Open()
                Dim dataReader As OleDbDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine( _
                        vbTab &amp; "{0}" &amp; vbTab &amp; "{1}" &amp; vbTab &amp; "{2}", _
                     dataReader(0), dataReader(1), dataReader(2))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.ReadLine()
        End Using
    End Sub
End Class
</code></pre><h3 id="odbc" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#odbc" aria-labelledby="odbc"></a><span data-ttu-id="a3bf0-129">Odbc</span><span class="sxs-lookup"><span data-stu-id="a3bf0-129">Odbc</span></span></h3>
<p><span data-ttu-id="a3bf0-130">此示例中的代码假定你可以连接到 Microsoft Access Northwind 示例数据库。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-130">The code in this example assumes that you can connect to the Microsoft Access Northwind sample database.</span></span> <span data-ttu-id="a3bf0-131">在此情形 5 中，示例代码创建一个 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbccommand" data-linktype="absolute-path">OdbcCommand</a> 以从 Products 表中选择行，并添加 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbcparameter" data-linktype="absolute-path">OdbcParameter</a> 来将结果限制为其 UnitPrice 大于指定参数值的行。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-131">The code creates a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbccommand" data-linktype="absolute-path">OdbcCommand</a> to select rows from the Products table, adding a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbcparameter" data-linktype="absolute-path">OdbcParameter</a> to restrict the results to rows with a UnitPrice greater than the specified parameter value, in this case 5.</span></span> <span data-ttu-id="a3bf0-132">在 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbcconnection" data-linktype="absolute-path">OdbcConnection</a> 块中打开 <code>using</code> ，这可确保在代码退出时关闭并释放资源。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-132">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbcconnection" data-linktype="absolute-path">OdbcConnection</a> is opened inside a <code>using</code> block, which ensures that resources are closed and disposed when the code exits.</span></span> <span data-ttu-id="a3bf0-133">示例代码使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbcdatareader" data-linktype="absolute-path">OdbcDataReader</a> 执行命令，并在控制台窗口中显示结果。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-133">The code executes the command by using a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.odbc.odbcdatareader" data-linktype="absolute-path">OdbcDataReader</a>, and displays the results in the console window.</span></span></p>
<div class="codeHeader" id="code-try-2" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" name="DataWorks SampleApp.Odbc#1" data-author-content="using System;
using System.Data;
using System.Data.Odbc;

class Program
{
    static void Main()
    {
        // The connection string assumes that the Access
        // Northwind.mdb is located in the c:\Data folder.
        string connectionString =
            &quot;Driver={Microsoft Access Driver (*.mdb)};&quot;
            + &quot;Dbq=c:\\Data\\Northwind.mdb;Uid=Admin;Pwd=;&quot;;

        // Provide the query string with a parameter placeholder.
        string queryString =
            &quot;SELECT ProductID, UnitPrice, ProductName from products &quot;
                + &quot;WHERE UnitPrice &gt; ? &quot;
                + &quot;ORDER BY UnitPrice DESC;&quot;;

        // Specify the parameter value.
        int paramValue = 5;

        // Create and open the connection in a using block. This
        // ensures that all resources will be closed and disposed
        // when the code exits.
        using (OdbcConnection connection =
            new OdbcConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            OdbcCommand command = new OdbcCommand(queryString, connection);
            command.Parameters.AddWithValue(&quot;@pricePoint&quot;, paramValue);

            // Open the connection in a try/catch block.
            // Create and execute the DataReader, writing the result
            // set to the console window.
            try
            {
                connection.Open();
                OdbcDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(&quot;\t{0}\t{1}\t{2}&quot;,
                        reader[0], reader[1], reader[2]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Data;
<span class="hljs-keyword">using</span> System.Data.Odbc;

<span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
{
    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-comment">// The connection string assumes that the Access</span>
        <span class="hljs-comment">// Northwind.mdb is located in the c:\Data folder.</span>
        <span class="hljs-keyword">string</span> connectionString =
            <span class="hljs-string">"Driver={Microsoft Access Driver (*.mdb)};"</span>
            + <span class="hljs-string">"Dbq=c:\\Data\\Northwind.mdb;Uid=Admin;Pwd=;"</span>;

        <span class="hljs-comment">// Provide the query string with a parameter placeholder.</span>
        <span class="hljs-keyword">string</span> queryString =
            <span class="hljs-string">"SELECT ProductID, UnitPrice, ProductName from products "</span>
                + <span class="hljs-string">"WHERE UnitPrice &gt; ? "</span>
                + <span class="hljs-string">"ORDER BY UnitPrice DESC;"</span>;

        <span class="hljs-comment">// Specify the parameter value.</span>
        <span class="hljs-keyword">int</span> paramValue = <span class="hljs-number">5</span>;

        <span class="hljs-comment">// Create and open the connection in a using block. This</span>
        <span class="hljs-comment">// ensures that all resources will be closed and disposed</span>
        <span class="hljs-comment">// when the code exits.</span>
        <span class="hljs-keyword">using</span> (OdbcConnection connection =
            <span class="hljs-keyword">new</span> OdbcConnection(connectionString))
        {
            <span class="hljs-comment">// Create the Command and Parameter objects.</span>
            OdbcCommand command = <span class="hljs-keyword">new</span> OdbcCommand(queryString, connection);
            command.Parameters.AddWithValue(<span class="hljs-string">"@pricePoint"</span>, paramValue);

            <span class="hljs-comment">// Open the connection in a try/catch block.</span>
            <span class="hljs-comment">// Create and execute the DataReader, writing the result</span>
            <span class="hljs-comment">// set to the console window.</span>
            <span class="hljs-keyword">try</span>
            {
                connection.Open();
                OdbcDataReader reader = command.ExecuteReader();
                <span class="hljs-keyword">while</span> (reader.Read())
                {
                    Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}\t{2}"</span>,
                        reader[<span class="hljs-number">0</span>], reader[<span class="hljs-number">1</span>], reader[<span class="hljs-number">2</span>]);
                }
                reader.Close();
            }
            <span class="hljs-keyword">catch</span> (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}
</span></code></pre><pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" name="DataWorks SampleApp.Odbc#1" data-author-content="Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.Odbc

Public Class Program
    Public Shared Sub Main()

        &#39; The connection string assumes that the Access 
        &#39; Northwind.mdb is located in the c:\Data folder.
        Dim connectionString As String = _
            &quot;Driver={Microsoft Access Driver (*.mdb)};&quot; _
           &amp; &quot;Dbq=c:\Data\Northwind.mdb;Uid=Admin;Pwd=;&quot;

        &#39; Provide the query string with a parameter placeholder.
        Dim queryString As String = _
            &quot;SELECT ProductID, UnitPrice, ProductName from Products &quot; _
            &amp; &quot;WHERE UnitPrice &gt; ? &quot; _
            &amp; &quot;ORDER BY UnitPrice DESC;&quot;

        &#39; Specify the parameter value.
        Dim paramValue As Integer = 5

        &#39; Create and open the connection in a using block. This
        &#39; ensures that all resources will be closed and disposed
        &#39; when the code exits.
        Using connection As New OdbcConnection(connectionString)

            &#39; Create the Command and Parameter objects.
            Dim command As New OdbcCommand(queryString, connection)
            command.Parameters.AddWithValue(&quot;@pricePoint&quot;, paramValue)

            &#39; Open the connection in a try/catch block. 
            &#39; Create and execute the DataReader, writing the result
            &#39; set to the console window.
            Try
                connection.Open()
                Dim dataReader As OdbcDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine( _
                        vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot; &amp; vbTab &amp; &quot;{2}&quot;, _
                     dataReader(0), dataReader(1), dataReader(2))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.ReadLine()
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.Odbc

Public Class Program
    Public Shared Sub Main()

        ' The connection string assumes that the Access 
        ' Northwind.mdb is located in the c:\Data folder.
        Dim connectionString As String = _
            "Driver={Microsoft Access Driver (*.mdb)};" _
           &amp; "Dbq=c:\Data\Northwind.mdb;Uid=Admin;Pwd=;"

        ' Provide the query string with a parameter placeholder.
        Dim queryString As String = _
            "SELECT ProductID, UnitPrice, ProductName from Products " _
            &amp; "WHERE UnitPrice &gt; ? " _
            &amp; "ORDER BY UnitPrice DESC;"

        ' Specify the parameter value.
        Dim paramValue As Integer = 5

        ' Create and open the connection in a using block. This
        ' ensures that all resources will be closed and disposed
        ' when the code exits.
        Using connection As New OdbcConnection(connectionString)

            ' Create the Command and Parameter objects.
            Dim command As New OdbcCommand(queryString, connection)
            command.Parameters.AddWithValue("@pricePoint", paramValue)

            ' Open the connection in a try/catch block. 
            ' Create and execute the DataReader, writing the result
            ' set to the console window.
            Try
                connection.Open()
                Dim dataReader As OdbcDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine( _
                        vbTab &amp; "{0}" &amp; vbTab &amp; "{1}" &amp; vbTab &amp; "{2}", _
                     dataReader(0), dataReader(1), dataReader(2))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.ReadLine()
        End Using
    End Sub
End Class
</code></pre><h3 id="oracleclient" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#oracleclient" aria-labelledby="oracleclient"></a><span data-ttu-id="a3bf0-134">OracleClient</span><span class="sxs-lookup"><span data-stu-id="a3bf0-134">OracleClient</span></span></h3>
<p><span data-ttu-id="a3bf0-135">此示例中的代码假定已建立与 Oracle 服务器上的 DEMO.CUSTOMER 的连接。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-135">The code in this example assumes a connection to DEMO.CUSTOMER on an Oracle server.</span></span> <span data-ttu-id="a3bf0-136">您还必须添加对 System.Data.OracleClient.dll 的引用。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-136">You must also add a reference to the System.Data.OracleClient.dll.</span></span> <span data-ttu-id="a3bf0-137">示例代码在 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oracleclient.oracledatareader" data-linktype="absolute-path">OracleDataReader</a> 中返回数据。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-137">The code returns the data in an <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.oracleclient.oracledatareader" data-linktype="absolute-path">OracleDataReader</a>.</span></span></p>
<div class="codeHeader" id="code-try-3" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" name="DataWorks SampleApp.Oracle#1" data-author-content="using System;
using System.Data;
using System.Data.OracleClient;

class Program
{
    static void Main()
    {
        string connectionString =
            &quot;Data Source=ThisOracleServer;Integrated Security=yes;&quot;;
        string queryString =
            &quot;SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER&quot;;
        using (OracleConnection connection =
                   new OracleConnection(connectionString))
        {
            OracleCommand command = connection.CreateCommand();
            command.CommandText = queryString;

            try
            {
                connection.Open();

                OracleDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine(&quot;\t{0}\t{1}&quot;,
                        reader[0], reader[1]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Data;
<span class="hljs-keyword">using</span> System.Data.OracleClient;

<span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
{
    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">string</span> connectionString =
            <span class="hljs-string">"Data Source=ThisOracleServer;Integrated Security=yes;"</span>;
        <span class="hljs-keyword">string</span> queryString =
            <span class="hljs-string">"SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER"</span>;
        <span class="hljs-keyword">using</span> (OracleConnection connection =
                   <span class="hljs-keyword">new</span> OracleConnection(connectionString))
        {
            OracleCommand command = connection.CreateCommand();
            command.CommandText = queryString;

            <span class="hljs-keyword">try</span>
            {
                connection.Open();

                OracleDataReader reader = command.ExecuteReader();

                <span class="hljs-keyword">while</span> (reader.Read())
                {
                    Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}"</span>,
                        reader[<span class="hljs-number">0</span>], reader[<span class="hljs-number">1</span>]);
                }
                reader.Close();
            }
            <span class="hljs-keyword">catch</span> (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
</span></code></pre><pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" name="DataWorks SampleApp.Oracle#1" data-author-content="Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.OracleClient

Public Class Program
    Public Shared Sub Main()

        Dim connectionString As String = _
            &quot;Data Source=ThisOracleServer;Integrated Security=yes;&quot;

        Dim queryString As String = _
            &quot;SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER&quot;

        Using connection As New OracleConnection(connectionString)
            Dim command As OracleCommand = connection.CreateCommand()
            command.CommandText = queryString
            Try
                connection.Open()
                Dim dataReader As OracleDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine(vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot;, _
                     dataReader(0), dataReader(1))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.OracleClient

Public Class Program
    Public Shared Sub Main()

        Dim connectionString As String = _
            "Data Source=ThisOracleServer;Integrated Security=yes;"

        Dim queryString As String = _
            "SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER"

        Using connection As New OracleConnection(connectionString)
            Dim command As OracleCommand = connection.CreateCommand()
            command.CommandText = queryString
            Try
                connection.Open()
                Dim dataReader As OracleDataReader = _
                 command.ExecuteReader()
                Do While dataReader.Read()
                    Console.WriteLine(vbTab &amp; "{0}" &amp; vbTab &amp; "{1}", _
                     dataReader(0), dataReader(1))
                Loop
                dataReader.Close()

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub
End Class
</code></pre><h2 id="entity-framework-examples" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#entity-framework-examples" aria-labelledby="entity-framework-examples"></a><span data-ttu-id="a3bf0-138">实体框架示例</span><span class="sxs-lookup"><span data-stu-id="a3bf0-138">Entity Framework examples</span></span></h2>
<p><span data-ttu-id="a3bf0-139">以下代码列表演示如何通过查询实体数据模型 (EDM) 中的实体来从数据源检索数据。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-139">The following code listings demonstrate how to retrieve data from a data source by querying entities in an Entity Data Model (EDM).</span></span> <span data-ttu-id="a3bf0-140">这些示例使用基于 Northwind 示例数据库的模型。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-140">These examples use a model based on the Northwind sample database.</span></span> <span data-ttu-id="a3bf0-141">有关实体框架的详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/overview" data-linktype="relative-path">实体框架概述</a>。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-141">For more information about Entity Framework, see <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/overview" data-linktype="relative-path">Entity Framework Overview</a>.</span></span></p>
<h3 id="linq-to-entities" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#linq-to-entities" aria-labelledby="linq-to-entities"></a><span data-ttu-id="a3bf0-142">LINQ to Entities</span><span class="sxs-lookup"><span data-stu-id="a3bf0-142">LINQ to Entities</span></span></h3>
<p><span data-ttu-id="a3bf0-143">此示例中的代码使用 LINQ 查询以 Categories 对象的形式返回数据，这些对象将作为仅包含 CategoryID 和 CategoryName 属性的匿名类型提取。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-143">The code in this example uses a LINQ query to return data as Categories objects, which are projected as an anonymous type that contains only the CategoryID and CategoryName properties.</span></span> <span data-ttu-id="a3bf0-144">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/language-reference/linq-to-entities" data-linktype="relative-path">LINQ to Entities 概述</a>。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-144">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/language-reference/linq-to-entities" data-linktype="relative-path">LINQ to Entities Overview</a>.</span></span></p>
<div class="codeHeader" id="code-try-4" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using System;
using System.Linq;
using System.Data.Objects;
using NorthwindModel;

class LinqSample
{
    public static void ExecuteQuery()
    {
        using (NorthwindEntities context = new NorthwindEntities())
        {
            try
            {
                var query = from category in context.Categories
                            select new
                            {
                                categoryID = category.CategoryID,
                                categoryName = category.CategoryName
                            };

                foreach (var categoryInfo in query)
                {
                    Console.WriteLine(&quot;\t{0}\t{1}&quot;,
                        categoryInfo.categoryID, categoryInfo.categoryName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Data.Objects;
<span class="hljs-keyword">using</span> NorthwindModel;

<span class="hljs-keyword">class</span> <span class="hljs-title">LinqSample</span>
{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ExecuteQuery</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">using</span> (NorthwindEntities context = <span class="hljs-keyword">new</span> NorthwindEntities())
        {
            <span class="hljs-keyword">try</span>
            {
                <span class="hljs-keyword">var</span> query = <span class="hljs-keyword">from</span> category <span class="hljs-keyword">in</span> context.Categories
                            <span class="hljs-keyword">select</span> <span class="hljs-keyword">new</span>
                            {
                                categoryID = category.CategoryID,
                                categoryName = category.CategoryName
                            };

                <span class="hljs-keyword">foreach</span> (<span class="hljs-keyword">var</span> categoryInfo <span class="hljs-keyword">in</span> query)
                {
                    Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}"</span>,
                        categoryInfo.categoryID, categoryInfo.categoryName);
                }
            }
            <span class="hljs-keyword">catch</span> (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
</span></code></pre>
<pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" data-author-content="Option Explicit On
Option Strict On

Imports System.Linq
Imports System.Data.Objects
Imports NorthwindModel

Class LinqSample
    Public Shared Sub ExecuteQuery()
        Using context As NorthwindEntities = New NorthwindEntities()
            Try
                Dim query = From category In context.Categories _
                            Select New With _
                            { _
                                .categoryID = category.CategoryID, _
                                .categoryName = category.CategoryName _
                            }

                For Each categoryInfo In query
                    Console.WriteLine(vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot;, _
                        categoryInfo.categoryID, categoryInfo.categoryName)
                Next
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Linq
Imports System.Data.Objects
Imports NorthwindModel

Class LinqSample
    Public Shared Sub ExecuteQuery()
        Using context As NorthwindEntities = New NorthwindEntities()
            Try
                Dim query = From category In context.Categories _
                            Select New With _
                            { _
                                .categoryID = category.CategoryID, _
                                .categoryName = category.CategoryName _
                            }

                For Each categoryInfo In query
                    Console.WriteLine(vbTab &amp; "{0}" &amp; vbTab &amp; "{1}", _
                        categoryInfo.categoryID, categoryInfo.categoryName)
                Next
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub
End Class
</code></pre>
<h3 id="typed-objectquery" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#typed-objectquery" aria-labelledby="typed-objectquery"></a><span data-ttu-id="a3bf0-145">类型化 ObjectQuery</span><span class="sxs-lookup"><span data-stu-id="a3bf0-145">Typed ObjectQuery</span></span></h3>
<p><span data-ttu-id="a3bf0-146">此示例中的代码使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.objects.objectquery-1" data-linktype="absolute-path">ObjectQuery&lt;T&gt;</a> 以 Categories 对象的形式返回数据。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-146">The code in this example uses an <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.objects.objectquery-1" data-linktype="absolute-path">ObjectQuery&lt;T&gt;</a> to return data as Categories objects.</span></span> <span data-ttu-id="a3bf0-147">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/bb896241(v=vs.100)" data-linktype="absolute-path">对象查询</a>。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-147">For more information, see <a href="https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/bb896241(v=vs.100)" data-linktype="absolute-path">Object Queries</a>.</span></span></p>
<div class="codeHeader" id="code-try-5" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using System;
using System.Data.Objects;
using NorthwindModel;

class ObjectQuerySample
{
    public static void ExecuteQuery()
    {
        using (NorthwindEntities context = new NorthwindEntities())
        {
            ObjectQuery&lt;Categories&gt; categoryQuery = context.Categories;

            foreach (Categories category in
                categoryQuery.Execute(MergeOption.AppendOnly))
            {
                Console.WriteLine(&quot;\t{0}\t{1}&quot;,
                    category.CategoryID, category.CategoryName);
            }
        }
    }
}
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Data.Objects;
<span class="hljs-keyword">using</span> NorthwindModel;

<span class="hljs-keyword">class</span> <span class="hljs-title">ObjectQuerySample</span>
{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ExecuteQuery</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">using</span> (NorthwindEntities context = <span class="hljs-keyword">new</span> NorthwindEntities())
        {
            ObjectQuery&lt;Categories&gt; categoryQuery = context.Categories;

            <span class="hljs-keyword">foreach</span> (Categories category <span class="hljs-keyword">in</span>
                categoryQuery.Execute(MergeOption.AppendOnly))
            {
                Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}"</span>,
                    category.CategoryID, category.CategoryName);
            }
        }
    }
}
</span></code></pre>
<pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" data-author-content="Option Explicit On
Option Strict On

Imports System.Data.Objects
Imports NorthwindModel

Class ObjectQuerySample
    Public Shared Sub ExecuteQuery()
        Using context As NorthwindEntities = New NorthwindEntities()
            Dim categoryQuery As ObjectQuery(Of Categories) = context.Categories

            For Each category As Categories In _
                categoryQuery.Execute(MergeOption.AppendOnly)
                Console.WriteLine(vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot;, _
                    category.CategoryID, category.CategoryName)
            Next
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Data.Objects
Imports NorthwindModel

Class ObjectQuerySample
    Public Shared Sub ExecuteQuery()
        Using context As NorthwindEntities = New NorthwindEntities()
            Dim categoryQuery As ObjectQuery(Of Categories) = context.Categories

            For Each category As Categories In _
                categoryQuery.Execute(MergeOption.AppendOnly)
                Console.WriteLine(vbTab &amp; "{0}" &amp; vbTab &amp; "{1}", _
                    category.CategoryID, category.CategoryName)
            Next
        End Using
    End Sub
End Class
</code></pre>
<h3 id="entityclient" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#entityclient" aria-labelledby="entityclient"></a><span data-ttu-id="a3bf0-148">EntityClient</span><span class="sxs-lookup"><span data-stu-id="a3bf0-148">EntityClient</span></span></h3>
<p><span data-ttu-id="a3bf0-149">此示例中的代码使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.entityclient.entitycommand" data-linktype="absolute-path">EntityCommand</a> 来执行实体 SQL 查询。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-149">The code in this example uses an <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.entityclient.entitycommand" data-linktype="absolute-path">EntityCommand</a> to execute an Entity SQL query.</span></span> <span data-ttu-id="a3bf0-150">此查询会返回表示 Categories 实体类型的实例的记录的列表。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-150">This query returns a list of records that represent instances of the Categories entity type.</span></span> <span data-ttu-id="a3bf0-151"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.entityclient.entitydatareader" data-linktype="absolute-path">EntityDataReader</a> 用于访问结果集中的数据记录。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-151">An <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.data.entityclient.entitydatareader" data-linktype="absolute-path">EntityDataReader</a> is used to access data records in the result set.</span></span> <span data-ttu-id="a3bf0-152">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/entityclient-provider-for-the-entity-framework" data-linktype="relative-path">用于 Entity Framework 的 EntityClient 提供程序</a>。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-152">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ef/entityclient-provider-for-the-entity-framework" data-linktype="relative-path">EntityClient Provider for the Entity Framework</a>.</span></span></p>
<div class="codeHeader" id="code-try-6" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using System;
using System.Data;
using System.Data.Common;
using System.Data.EntityClient;
using NorthwindModel;

class EntityClientSample
{
    public static void ExecuteQuery()
    {
        string queryString =
            @&quot;SELECT c.CategoryID, c.CategoryName
                FROM NorthwindEntities.Categories AS c&quot;;

        using (EntityConnection conn =
            new EntityConnection(&quot;name=NorthwindEntities&quot;))
        {
            try
            {
                conn.Open();
                using (EntityCommand query = new EntityCommand(queryString, conn))
                {
                    using (DbDataReader rdr =
                        query.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        while (rdr.Read())
                        {
                            Console.WriteLine(&quot;\t{0}\t{1}&quot;, rdr[0], rdr[1]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Data;
<span class="hljs-keyword">using</span> System.Data.Common;
<span class="hljs-keyword">using</span> System.Data.EntityClient;
<span class="hljs-keyword">using</span> NorthwindModel;

<span class="hljs-keyword">class</span> <span class="hljs-title">EntityClientSample</span>
{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ExecuteQuery</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">string</span> queryString =
            <span class="hljs-string">@"SELECT c.CategoryID, c.CategoryName
                FROM NorthwindEntities.Categories AS c"</span>;

        <span class="hljs-keyword">using</span> (EntityConnection conn =
            <span class="hljs-keyword">new</span> EntityConnection(<span class="hljs-string">"name=NorthwindEntities"</span>))
        {
            <span class="hljs-keyword">try</span>
            {
                conn.Open();
                <span class="hljs-keyword">using</span> (EntityCommand query = <span class="hljs-keyword">new</span> EntityCommand(queryString, conn))
                {
                    <span class="hljs-keyword">using</span> (DbDataReader rdr =
                        query.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        <span class="hljs-keyword">while</span> (rdr.Read())
                        {
                            Console.WriteLine(<span class="hljs-string">"\t{0}\t{1}"</span>, rdr[<span class="hljs-number">0</span>], rdr[<span class="hljs-number">1</span>]);
                        }
                    }
                }
            }
            <span class="hljs-keyword">catch</span> (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
</span></code></pre>
<pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" data-author-content="Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.Common
Imports System.Data.EntityClient
Imports NorthwindModel

Class EntityClientSample
    Public Shared Sub ExecuteQuery()
        Dim queryString As String = _
            &quot;SELECT c.CategoryID, c.CategoryName &quot; &amp; _
                &quot;FROM NorthwindEntities.Categories AS c&quot;

        Using conn As EntityConnection = _
            New EntityConnection(&quot;name=NorthwindEntities&quot;)

            Try
                conn.Open()
                Using query As EntityCommand = _
                New EntityCommand(queryString, conn)
                    Using rdr As DbDataReader = _
                        query.ExecuteReader(CommandBehavior.SequentialAccess)
                        While rdr.Read()
                            Console.WriteLine(vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot;, _
                                              rdr(0), rdr(1))
                        End While
                    End Using
                End Using
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.Common
Imports System.Data.EntityClient
Imports NorthwindModel

Class EntityClientSample
    Public Shared Sub ExecuteQuery()
        Dim queryString As String = _
            "SELECT c.CategoryID, c.CategoryName " &amp; _
                "FROM NorthwindEntities.Categories AS c"

        Using conn As EntityConnection = _
            New EntityConnection("name=NorthwindEntities")

            Try
                conn.Open()
                Using query As EntityCommand = _
                New EntityCommand(queryString, conn)
                    Using rdr As DbDataReader = _
                        query.ExecuteReader(CommandBehavior.SequentialAccess)
                        While rdr.Read()
                            Console.WriteLine(vbTab &amp; "{0}" &amp; vbTab &amp; "{1}", _
                                              rdr(0), rdr(1))
                        End While
                    End Using
                End Using
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub
End Class
</code></pre>
<h2 id="linq-to-sql" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#linq-to-sql" aria-labelledby="linq-to-sql"></a><span data-ttu-id="a3bf0-153">LINQ to SQL</span><span class="sxs-lookup"><span data-stu-id="a3bf0-153">LINQ to SQL</span></span></h2>
<p><span data-ttu-id="a3bf0-154">此示例中的代码使用 LINQ 查询以 Categories 对象的形式返回数据，这些对象将作为仅包含 CategoryID 和 CategoryName 属性的匿名类型提取。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-154">The code in this example uses a LINQ query to return data as Categories objects, which are projected as an anonymous type that contains only the CategoryID and CategoryName properties.</span></span> <span data-ttu-id="a3bf0-155">此示例基于 Northwind 数据上下文。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-155">This example is based on the Northwind data context.</span></span> <span data-ttu-id="a3bf0-156">有关详细信息，请参阅<a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/sql/linq/getting-started" data-linktype="relative-path">入门</a>。</span><span class="sxs-lookup"><span data-stu-id="a3bf0-156">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/sql/linq/getting-started" data-linktype="relative-path">Getting Started</a>.</span></span></p>
<div class="codeHeader" id="code-try-7" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Northwind;

    class LinqSqlSample
    {
        public static void ExecuteQuery()
        {
            using (NorthwindDataContext db = new NorthwindDataContext())
            {
                try
                {
                    var query = from category in db.Categories
                                select new
                                {
                                    categoryID = category.CategoryID,
                                    categoryName = category.CategoryName
                                };

                    foreach (var categoryInfo in query)
                    {
                        Console.WriteLine(&quot;vbTab {0} vbTab {1}&quot;,
                            categoryInfo.categoryID, categoryInfo.categoryName);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Collections.Generic;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Text;
<span class="hljs-keyword">using</span> Northwind;

    <span class="hljs-keyword">class</span> <span class="hljs-title">LinqSqlSample</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ExecuteQuery</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">using</span> (NorthwindDataContext db = <span class="hljs-keyword">new</span> NorthwindDataContext())
            {
                <span class="hljs-keyword">try</span>
                {
                    <span class="hljs-keyword">var</span> query = <span class="hljs-keyword">from</span> category <span class="hljs-keyword">in</span> db.Categories
                                <span class="hljs-keyword">select</span> <span class="hljs-keyword">new</span>
                                {
                                    categoryID = category.CategoryID,
                                    categoryName = category.CategoryName
                                };

                    <span class="hljs-keyword">foreach</span> (<span class="hljs-keyword">var</span> categoryInfo <span class="hljs-keyword">in</span> query)
                    {
                        Console.WriteLine(<span class="hljs-string">"vbTab {0} vbTab {1}"</span>,
                            categoryInfo.categoryID, categoryInfo.categoryName);
                    }
                }
                <span class="hljs-keyword">catch</span> (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
</span></code></pre>
<pre tabindex="0" class="has-inner-focus" hidden=""><code class="lang-vb" data-author-content="Option Explicit On
Option Strict On

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Northwind

Class LinqSqlSample
    Public Shared Sub ExecuteQuery()
        Using db As NorthwindDataContext = New NorthwindDataContext()
            Try
                Dim query = From category In db.Categories _
                                Select New With _
                                { _
                                    .categoryID = category.CategoryID, _
                                    .categoryName = category.CategoryName _
                                }

                For Each categoryInfo In query
                    Console.WriteLine(vbTab &amp; &quot;{0}&quot; &amp; vbTab &amp; &quot;{1}&quot;, _
                            categoryInfo.categoryID, categoryInfo.categoryName)
                Next
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            End Using
    End Sub
End Class
">Option Explicit On
Option Strict On

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Northwind

Class LinqSqlSample
    Public Shared Sub ExecuteQuery()
        Using db As NorthwindDataContext = New NorthwindDataContext()
            Try
                Dim query = From category In db.Categories _
                                Select New With _
                                { _
                                    .categoryID = category.CategoryID, _
                                    .categoryName = category.CategoryName _
                                }

                For Each categoryInfo In query
                    Console.WriteLine(vbTab &amp; "{0}" &amp; vbTab &amp; "{1}", _
                            categoryInfo.categoryID, categoryInfo.categoryName)
                Next
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            End Using
    End Sub
End Class
</code></pre>
<h2 id="see-also" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#see-also" aria-labelledby="see-also"></a><span data-ttu-id="a3bf0-157">请参阅</span><span class="sxs-lookup"><span data-stu-id="a3bf0-157">See also</span></span></h2>
<ul>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-overview" data-linktype="relative-path"><span data-ttu-id="a3bf0-158">ADO.NET 概述</span><span class="sxs-lookup"><span data-stu-id="a3bf0-158">ADO.NET Overview</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/retrieving-and-modifying-data" data-linktype="relative-path"><span data-ttu-id="a3bf0-159">在 ADO.NET 中检索和修改数据</span><span class="sxs-lookup"><span data-stu-id="a3bf0-159">Retrieving and Modifying Data in ADO.NET</span></span></a></li>
<li><span data-ttu-id="a3bf0-160"><a href="https://docs.microsoft.com/zh-cn/previous-versions/visualstudio/visual-studio-2013/h0y4a0f6(v=vs.120)" data-linktype="absolute-path">创建数据应用程序</a></span><span class="sxs-lookup"><span data-stu-id="a3bf0-160"><a href="https://docs.microsoft.com/zh-cn/previous-versions/visualstudio/visual-studio-2013/h0y4a0f6(v=vs.120)" data-linktype="absolute-path">Creating Data Applications</a></span></span></li>
<li><span data-ttu-id="a3bf0-161"><a href="https://docs.microsoft.com/zh-cn/previous-versions/bb738455(v=vs.90)" data-linktype="absolute-path">查询实体数据模型（实体框架任务）</a></span><span class="sxs-lookup"><span data-stu-id="a3bf0-161"><a href="https://docs.microsoft.com/zh-cn/previous-versions/bb738455(v=vs.90)" data-linktype="absolute-path">Querying an Entity Data Model (Entity Framework Tasks)</a></span></span></li>
<li><span data-ttu-id="a3bf0-162"><a href="https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/bb738512(v=vs.100)" data-linktype="absolute-path">如何：执行返回匿名类型对象的查询</a></span><span class="sxs-lookup"><span data-stu-id="a3bf0-162"><a href="https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/bb738512(v=vs.100)" data-linktype="absolute-path">How to: Execute a Query that Returns Anonymous Type Objects</a></span></span></li>
</ul>

						<!-- </content> -->

						</main>

						<!-- page rating section -->
								<div class="is-hidden-desktop has-border-top has-margin-top-large has-padding-top-large binary-rating-holder">
									
	
	

	<div class="feedback-verbatim has-border-bottom has-padding-bottom-large has-margin-bottom-small" data-bi-name="rating">
    <div class="binary-rating">
        <div class="binary-rating-buttons">
				<h3 class="has-text-weight-semibold has-margin-top-none has-margin-bottom-small is-size-h4 has-margin-bottom-medium">此页面有帮助吗?</h3>
			<div>
                <button class="thumb-rating like has-inner-focus" title="是" data-bi-name="rating-yes" aria-expanded="false" data-bi-sat="1" aria-controls="rating-container-desktop">
                    <span aria-hidden="true" class="icon docon docon-like"></span>
                    <span>是</span>
                </button>
                <button class="thumb-rating dislike has-inner-focus" title="否" data-bi-name="rating-no" data-bi-sat="0" aria-expanded="false" aria-controls="rating-container-desktop">
                    <span aria-hidden="true" class="icon docon docon-dislike"></span>
                    <span>否</span>
                </button>
            </div>
        </div>
        <form class="feedback-verbatim-form is-hidden" id="rating-container-desktop">
            <div class="verbatim-textarea box is-relative has-box-shadow-none has-border has-margin-top-small has-padding-extra-small is-size-extra-small">
                <label for="rating-textarea-desktop" class="visually-hidden">还有其他反馈吗？</label>
                <textarea id="rating-textarea-desktop" rows="4" maxlength="999" placeholder="还有其他反馈吗？" required="" class="textarea has-border-none has-box-shadow-none has-inner-focus"></textarea>
            </div>
            <div class="buttons is-right has-margin-top-medium has-margin-right-extra-small">
                <button class="skip-rating button is-transparent has-text-primary is-small has-border-none" type="button">跳过</button>
                <button class="submit-rating button is-primary is-small" data-bi-name="rating-verbatim" disabled="" type="submit">提交</button>
            </div>
        </form>
    </div>
    <div class="thankyou-rating is-hidden" tabindex="-1">
        <p>谢谢。</p>
    </div>
</div>									</div>
						<!-- end page rating section -->


						<!-- recommendations section -->
						<!-- end recommendations section -->

						<!-- feedback section -->
						<!-- end feedback section -->

						<!-- feedback report section -->
						<!-- end feedback report section -->

						<div class="footerContainer is-visible-interactive has-default-focus ">



	<footer id="footer-interactive" data-bi-name="footer" class="footer-layout">

    <div class="is-flex is-full-height has-padding-right-extra-large-desktop">
			<a data-mscc-ic="false" class="locale-selector-link has-flex-shrink-none" href="https://docs.microsoft.com/zh-cn/locale?target=https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#sqlclient" data-bi-name="select-locale"><span class="icon docon docon-world is-size-large has-margin-right-small" aria-hidden="true"></span><span class="local-selector-link-text">中文 (简体)</span></a>
		<div class="has-margin-left-medium has-margin-right-medium has-flex-shrink-none">
<div class="dropdown has-caret-up">
	<button class="dropdown-trigger button is-transparent is-small is-icon-only-touch has-inner-focus theme-dropdown-trigger" aria-controls="theme-menu-interactive" aria-expanded="false" title="主题" data-bi-name="theme">
		<span class="icon">
			<span class="docon docon-sun" aria-hidden="true"></span>
		</span>
		<span>主题</span>
	</button>
	<div class="dropdown-menu" id="theme-menu-interactive" role="menu">
		<ul class="theme-selector has-padding-small">
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start is-selected" data-theme-to="light" aria-pressed="true">
					<span class="theme-light has-margin-right-small">
						<span class="theme-selector-icon css-variable-support has-border is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
亮					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="dark" aria-pressed="false">
					<span class="theme-dark has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
暗					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="high-contrast" aria-pressed="false">
					<span class="theme-high-contrast has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
高对比度					</span>
				</button>
			</li>
		</ul>
	</div>
</div>		</div>
	</div>
    <ul class="links" data-bi-name="footerlinks">
		<li class="manage-cookies-holder" hidden=""></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/previous-versions/" data-bi-name="archivelink">旧版文档</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/teamblog" data-bi-name="bloglink">博客</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/contribute" data-bi-name="contributorGuide">参与</a></li>
				<li><a data-mscc-ic="false" href="https://go.microsoft.com/fwlink/?LinkId=521839" data-bi-name="privacy">隐私和 Cookie</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/legal/termsofuse" data-bi-name="termsofuse">使用条款</a></li>
				<li><a data-mscc-ic="false" href="https://aka.ms/sitefeedback" data-bi-name="feedback">网站反馈</a></li>
			<li><a data-mscc-ic="false" href="https://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/EN-US.aspx" data-bi-name="trademarks">商标</a></li>
		<li>© Microsoft 2021</li>
    </ul>
</footer>
						</div>
					</div>

					<div class="is-size-small right-container column is-one-quarter is-one-fifth-desktop is-hidden-mobile is-hidden-tablet-only" data-bi-name="pageactions" role="complementary" aria-label="页面操作">
						<div id="affixed-right-container" class="doc-outline is-fixed is-vertically-scrollable" style="width: 172.938px; top: 24px; bottom: 24px;">
								
	
	

	<div class="feedback-verbatim has-border-bottom has-padding-bottom-small has-margin-bottom-small" data-bi-name="rating">
    <div class="binary-rating">
        <div class="binary-rating-buttons">

				<h3 class="has-text-weight-semibold has-margin-top-none has-margin-bottom-small">此页面有帮助吗?</h3>
			<div>
                <button class="thumb-rating like has-inner-focus" title="是" data-bi-name="rating-yes" aria-expanded="false" data-bi-sat="1" aria-controls="rating-container-mobile">
                    <span aria-hidden="true" class="icon docon docon-like"></span>
                    <span>是</span>
                </button>
                <button class="thumb-rating dislike has-inner-focus" title="否" data-bi-name="rating-no" data-bi-sat="0" aria-expanded="false" aria-controls="rating-container-mobile">
                    <span aria-hidden="true" class="icon docon docon-dislike"></span>
                    <span>否</span>
                </button>
            </div>
        </div>
        <form class="feedback-verbatim-form is-hidden" id="rating-container-mobile">
            <div class="verbatim-textarea box is-relative has-box-shadow-none has-border has-margin-top-small has-padding-extra-small is-size-extra-small">
                <label for="rating-textarea-mobile" class="visually-hidden">还有其他反馈吗？</label>
                <textarea id="rating-textarea-mobile" rows="4" maxlength="999" placeholder="还有其他反馈吗？" required="" class="textarea has-border-none has-box-shadow-none has-inner-focus"></textarea>
            </div>
            <div class="buttons is-right has-margin-top-medium has-margin-right-extra-small">
                <button class="skip-rating button is-transparent has-text-primary is-small has-border-none" type="button">跳过</button>
                <button class="submit-rating button is-primary is-small" data-bi-name="rating-verbatim" disabled="" type="submit">提交</button>
            </div>
        </form>
    </div>
    <div class="thankyou-rating is-hidden" tabindex="-1">
        <p>谢谢。</p>
    </div>
</div>							<nav id="side-doc-outline" data-bi-name="intopic toc" role="navigation" aria-label="文章大纲">
								<h3>本文内容</h3>
							<ol><li class="selected"><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#adonet-data-provider-examples">ADO.NET 数据提供程序示例</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#entity-framework-examples">实体框架示例</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#linq-to-sql">LINQ to SQL</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#see-also">请参阅</a></li></ol></nav>
						</div>
					</div>

					<!--end of div.columns -->
				</div>

			<!--end of .primary-holder -->
			</section>

			<aside id="interactive-container" class="interactive-container is-visible-interactive column has-body-background-dark ">
			</aside>
		</div>

		<!--end of .mainContainer -->
	</div>

	<div class="footerContainer has-default-focus is-hidden-interactive ">



	<footer id="footer" data-bi-name="footer" class="footer-layout uhf-container has-padding" role="contentinfo">

    <div class="is-flex is-full-height has-padding-right-extra-large-desktop">
			<a data-mscc-ic="false" class="locale-selector-link has-flex-shrink-none" href="https://docs.microsoft.com/zh-cn/locale?target=https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/ado-net-code-examples#sqlclient" data-bi-name="select-locale"><span class="icon docon docon-world is-size-large has-margin-right-small" aria-hidden="true"></span><span class="local-selector-link-text">中文 (简体)</span></a>
		<div class="has-margin-left-medium has-margin-right-medium has-flex-shrink-none">
<div class="dropdown has-caret-up">
	<button class="dropdown-trigger button is-transparent is-small is-icon-only-touch has-inner-focus theme-dropdown-trigger" aria-controls="theme-menu" aria-expanded="false" title="主题" data-bi-name="theme">
		<span class="icon">
			<span class="docon docon-sun" aria-hidden="true"></span>
		</span>
		<span>主题</span>
	</button>
	<div class="dropdown-menu" id="theme-menu" role="menu">
		<ul class="theme-selector has-padding-small">
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start is-selected" data-theme-to="light" aria-pressed="true">
					<span class="theme-light has-margin-right-small">
						<span class="theme-selector-icon css-variable-support has-border is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
亮					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="dark" aria-pressed="false">
					<span class="theme-dark has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
暗					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="high-contrast" aria-pressed="false">
					<span class="theme-high-contrast has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
高对比度					</span>
				</button>
			</li>
		</ul>
	</div>
</div>		</div>
	</div>
    <ul class="links" data-bi-name="footerlinks">
		<li class="manage-cookies-holder" hidden=""></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/previous-versions/" data-bi-name="archivelink">旧版文档</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/teamblog" data-bi-name="bloglink">博客</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/contribute" data-bi-name="contributorGuide">参与</a></li>
				<li><a data-mscc-ic="false" href="https://go.microsoft.com/fwlink/?LinkId=521839" data-bi-name="privacy">隐私和 Cookie</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/legal/termsofuse" data-bi-name="termsofuse">使用条款</a></li>
				<li><a data-mscc-ic="false" href="https://aka.ms/sitefeedback" data-bi-name="feedback">网站反馈</a></li>
			<li><a data-mscc-ic="false" href="https://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/EN-US.aspx" data-bi-name="trademarks">商标</a></li>
		<li>© Microsoft 2021</li>
    </ul>
</footer>
	</div>

		<script type="text/x-mathjax-config;executed=true">
			MathJax.Hub.Config({
				showMathMenu: false,
				showMathMenuMSIE: false,
				tex2jax: {
					inlineMath: [['$','$'], ['\\(','\\)']],
					processEscapes: true
				}
			});
			MathJax.Hub.Register.StartupHook("End", function () {
				document.documentElement.classList.add('mathjax-complete');
			});
		</script>
		<script src="./代码示例 - ADO.NET _ Microsoft Docs_files/MathJax.js.下载" integrity="sha384-RLsAi7wiVvnmHrRjlVvUjU45C0bfsIBg6Gsxue6azjJi+lA5q5uK+XbGzO3yn5g1" crossorigin="anonymous"></script>
	<div id="action-panel" role="region" aria-label="操作面板" class="action-panel has-default-focus" tabindex="-1"></div>


<div class="usabilla_live_button_container" role="button" tabindex="0" style="margin-top: 0px; position: fixed; width: 0px; height: 0px; z-index: 99999990; right: 0px; top: 50%; display: none;" aria-label="Usabilla Feedback Button"><iframe src="./代码示例 - ADO.NET _ Microsoft Docs_files/saved_resource(2).html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" data-tags="right" title="Usabilla Feedback Button" style="width: 0px; height: 0px; margin: 0px; padding: 0px; border: 0px; overflow: hidden; z-index: 9998; position: absolute; left: 0px; top: 0px; box-shadow: 0px 0px 0px; background-color: transparent;"></iframe></div></body></html>